Results 1 to 5 of 5

Thread: PHP: setcookie() problem

  1. #1
    Junior Member
    Join Date
    Aug 2004
    Posts
    25

    Unhappy PHP: setcookie() problem

    I hav been tryin 2 create a cookie from a php script, bt the cookie is not being created!

    I hav search loads and loads of forums and web-pages 2 try 2 find the answer and dispite learning a lot about creating cookies using php I hav not been able 2 find a solution 2 whatever is causing my problem.

    So in wh@ feals like desperation I come to AO in order 4 sum1 2 tell me wh@ I hav been doing wrong . The piece of code that I am tryin 2 create to cookie using is shown below:

    PHP Code:
    setcookie("example.co.uk.login.usr",$_POST["usr"]); 
    Any & all assistance will b greatfully recieved.

    Thank You,
    TTAYO

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Ope u r fine and I can tell u wh@ u r doin wron'

    Cookie information cannot be sent after any text has been displayed[1].
    That's one of the most common mistakes. Test for it!
    The following code works on my php test machine (PHP 4.2.3).

    Code:
    <?php
    if (!isset($_COOKIE['cookie']))	{
            setcookie ("cookie[three]", "cookiethree");
    	setcookie ("cookie[two]", "cookietwo");
    	setcookie ("cookie[one]", "cookieone");
    
        echo("Never ever echo something before setting the cookies!<br>");
        echo("Refresh to show the content of cookies.<br><hr>");
    }	
    
    
    if (isset($_COOKIE['cookie'])){
    	
       foreach ($_COOKIE['cookie'] as $name => $value) {
            echo "$name : $value <br />\n";
        }
    
        print_r($_COOKIE);
    }
    	
     phpinfo();
    ?>

    Cheers

    [1] http://us4.php.net/setcookie
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Junior Member
    Join Date
    Aug 2004
    Posts
    25
    Thanx 4 ur reply sec_ware!

    Although I do call the setcookie() function b4 I echo any output to the browser.

    I am using a Windows XP pro box, and IE to attempt to view my web-pages, therefore I am not sure whether it is sumthin to do with IE th@ is stoppin me from creatin cookies on my machine (hence the many "." in the cookie name).

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi TTAYO

    Interesting. Let's track down the problem:

    1) Client-side: Have you disabled cookies on IE?
    2) Client-side: Have you tried firefox (user cookie management is superior to IE in my opinion)?
    3) Server-side: What is your WebServer and PHP version?
    4) Server-side: Have you tried my example (cut and paste into a .php file and load it in IE (no <HTML> etc)?
    5) Server-side: Could you attach (as txt-file) the full content of your server-side setcookie-file?
    6) Client-side: You can try to intercept the server-client communication[1]?

    Cheers

    [1] http://osx.freshmeat.net/projects/burpproxy/
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  5. #5
    Junior Member
    Join Date
    Aug 2004
    Posts
    25
    Sorry th@ it has taken some time to reply!

    I hav found out th@ the problem with my script wz th@ I wz not setting an experation time, because as long as I set and experation time then the script does create a cookie on my machine.

    I not sure if this is a limitation of IE or of the web host th@ I am using, bt the problem is now solved.

    Thanx 4 ur help anyway,
    L8R, TTAYO

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •