Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: php cookies

  1. #11
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    In the valid everything being outputted to the browser is outputted after the header declaration. In the invalids things are being outputted before the header declaration. If you provide your full code we can be more specific, you can censor out secure parts with '****'.
    Corrections to my coding/thoughts welcome.

  2. #12
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    perhaps a little clarification on how webpages are served.

    The headers come first in the server response. after they're done, there is a blank line, and then the content of the webpage follows (which the browser renders). For example:
    Code:
     HTTP/1.1 200 OK
     Date: Mon, 23 May 2005 22:38:34 GMT 
     Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) 
     Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT 
     Etag: "3f80f-1b6-3e1cb03b" 
     Accept-Ranges: bytes 
     Content-Length: 438 
     Connection: close 
     Content-Type: text/html; charset=UTF-8 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta name="generator" content="vBulletin 3.8.1" />
    
    <meta name="keywords" content=" php cookies, dhtml, javascript, css, dynamic html, xml, html, php, cgi" />
    <meta name="description" content="Page 2- php cookies PHP" />
    
    <link rel="shortcut icon" href="favicon.ico" />
    <!-- blah blah blah etc etc..........-->
    in this example, the last header is the content-type header. the blank line below signals the end of the headers, and the following line (in this case, with the doctype) is the first line of content.

    once php outputs anything that is not a header (e.g., text, error messages, html markup, even whitespace or html comments), then the headers end on no more headers can be sent. When you try to use a function that sets headers (like header() or setcookie()) after content has been output, you get the "...headers already sent" error, because you can't "back up" and serve another header after that section of the server response is complete.

  3. #13
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I have a different question but on the same cookie. This code
    PHP Code:
    <?php
    $george 
    60 60  time(); 
    setcookie('Authorization','no'$george); 
    ?>
    I tried to set the timeout to 60 minutes. Did I get it right?

  4. #14
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    yup.
    just think: seconds,minutes,hours,days, ...etc
    PHP Code:
    60 /*seconds*/ 60 /*minutes*/ 24 /*hours*/ 365 /*days*/ 

  5. #15
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I have 1 more question

    for this code
    PHP Code:
    <?php
    if(isset($_COOKIE['Authorization']))
        
    $cheese $_POST['name']; 
        
    $inTwoMonths 60 60 24 60 time();  
        
    setcookie('lastVisit',$cheese$inTwoMonths);
    else
        
    header('location: index.php');
    ?>
    it comes up with this error
    Parse error: syntax error, unexpected T_ELSE in /home1/keyboard/public_html/setcookie.php on line 6


    I don't understand what i've done wrong any help would be appreciated

  6. #16
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You need curly braces if your conditional is more than 1 line

    PHP Code:
    <?php
    if(isset($_COOKIE['Authorization'])) {
        
    $cheese $_POST['name']; 
        
    $inTwoMonths 60 60 24 60 time();  
        
    setcookie('lastVisit',$cheese$inTwoMonths);
    } else
        
    header('location: index.php');
    ?>
    This would also work.

    PHP Code:
    <?php
    if(isset($_COOKIE['Authorization'])) {
        
    $cheese $_POST['name']; 
        
    $inTwoMonths 60 60 24 60 time();  
        
    setcookie('lastVisit',$cheese$inTwoMonths);
    } else {
        
    header('location: index.php');
    }
    ?>
    The curly braces aren't necessary if what the conditional controls is only one line.
    Corrections to my coding/thoughts welcome.

  7. #17
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks, it now works perfectly. How can I set this thread to resolved

  8. #18
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Go to your first post (http://www.dynamicdrive.com/forums/s...26&postcount=1), click edit, click go advanced, add the resolved prefix to the thread title, and you should be all set.
    Corrections to my coding/thoughts welcome.

  9. #19
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by bluewalrus View Post
    The curly braces aren't necessary if what the conditional controls is only one line.
    Still, I always use them (and recommend the same to everyone).

  10. #20
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I clicked on the link but there is no edit option, there is for my other posts and yes, i am logged in.

Bookmarks

Posting Permissions

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