Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: php cookies

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default php cookies

    Hi everyone,

    I have been fidling with php cookies and came up with this

    PHP Code:
    <?php
    if(isset($_COOKIE['authorization']))
        
    header('location: index.php'); 
    else
        
    ?>


    <?php
    $cheese 
    $_POST['name'];
    $inTwoMonths 60 60 24 60 time(); 
    setcookie('lastVisit',$cheese$inTwoMonths); 
    ?>
    <html>
    <head>
    <meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
    </head>

    <body>
    <center>
    <p><font size="+2">Thankyou, you will be redirected shortly</font></p>
    <p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
    </center>
    </body>
    </html>

    The idea behind it is to gather input from a form and save that input as a cookie. However I also wanted to make sure that they are coming from the page that has the form on it. If not they are redirected to a different page. It comes up with an error. Is it because I have two cookie thingys on one page. Any help would be appreciated

  2. #2
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Without seeing the exact error its hard to say.

    Try this.
    PHP Code:
    <?php
    if(isset($_COOKIE['authorization']))
        
    header('location: index.php'); 
    else{
        
    $cheese $_POST['name'];
        
    $inTwoMonths 60 60 24 60 time(); 
        
    setcookie('lastVisit',$cheese$inTwoMonths); 
    ?>
    <html>
    <head>
    <meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
    </head>

    <body>
    <center>
    <p><font size="+2">Thankyou, you will be redirected shortly</font></p>
    <p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
    </center>
    </body>
    </html>
    <?php
    }
    ?>
    You might also want some validation against the $_POST['name'].

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

    Default

    Your question is unclear.
    Please provide more information, and be as specific as possible. What do you want to accomplish? What have you already tried? What problems did you encounter?
    Also, please be sure that you have included all relevant code and/or a link to the page in question.


    Please post the content of the error you are receiving.

    If there is any other code which affects this script, please post that as well (for example, you're trying to check for a cookie named "authorization," but you don't show the code that sets that cookie. Your html form is missing as well).

    I have no idea what you mean by "cookie thingys" (which, BTW, should be spelled thingies).

    Is this the same code you were trying to develop in your other thread?
    If so, you should post there; it helps keep things organized and will get you a better-informed response.
    Last edited by traq; 07-07-2011 at 08:24 PM.

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

    Default

    The first page of my website is this



    PHP Code:
    <?php
    if(isset($_COOKIE['lastVisit']))
        
    header('location: indexthesecond.php'); 
    else
        
    $visit $_COOKIE['lastVisit'];
    ?>

    <html>
    <head>
    <title>Randomhelp4you</title>

    <?php
    file_put_contents
    ('ips.log'$_SERVER['REMOTE_ADDR'] . "\n"FILE_APPEND);
    ?> 

    <?php
    $george 
    60 60  time(); 
    setcookie('Authorization','no'$george); 
    ?>
    <body style="background-color:lightblue">

    </head>
    <body>

    <form action="setcookie.php" method="post"> 

    Name <input name="name" type="text" maxlength="10" /> 
    <input type="submit"/>
    </form>




    </body>
    </html>


    The second page looks like this


    PHP Code:
    <?php
    if(isset($_COOKIE['authorization']))
        
    header('location: index.php'); 
    else
        
    ?>


    <?php
    $cheese 
    $_POST['name'];
    $inTwoMonths 60 60 24 60 time(); 
    setcookie('lastVisit',$cheese$inTwoMonths); 
    ?>
    <html>
    <head>
    <meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
    </head>

    <body>
    <center>
    <p><font size="+2">Thankyou, you will be redirected shortly</font></p>
    <p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
    </center>
    </body>
    </html>
    and the third page looks like this

    PHP Code:
    <?php
    if(isset($_COOKIE['lastVisit']))
        
    $visit $_COOKIE['lastVisit'];
    else
        
    header('location: index.php');
    ?>

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="basic.css" />

    <title>Randomhelp4you</title>



    </script>

    <body style="background-color:lightblue">

    </head>
    <body>
    <h1><center>Randomhelp4you</center></h1>

    <?php
    echo "Hey "$visit;
    ?>


    </body>
    </html>


    The idea behind it is that the go onto he index page (php) and enter their name. This form is procesed by setcookie.php . This page sets a cookie with their name on it, then redirects them to indexthesecond.php , were it displays their name. If they try to acess indexthesecond.php without the cookie, they should be redirected to index.php . If they try to go onto index.php with the cookie then they are redirected to indexthesecond.php . This all works fine but I am trying to add in an extra cookie with a 60 minute timeout which is assigned by index.php . This is to make sure that when they go to setcookie.php they came from index.php . I'm trying to explain this easily but if it dosen't make sense just tell me.

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

    Default

    The error on index.php is

    Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/index.php:12) in /home1/keyboard/public_html/index.php on line 18

    The error on setcookie.php is

    Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/setcookie.php:9) in /home1/keyboard/public_html/setcookie.php on line 12

    By the way i did try the code that joedastudd posted (thankyou) but I could still acess the setcookie.php page without the cookie set by index.php.
    Also joedastudd suggested that I add validation against $_POST['name']. I have no idea how to do this. Could anyone give me some tips please.
    Last edited by keyboard; 07-09-2011 at 12:52 AM. Reason: added something in

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

    Default

    Is this line 12
    PHP Code:
    header('location: indexthesecond.php'); 
    If so what are lines 1-10 you can't output any content before you use the header.
    Corrections to my coding/thoughts welcome.

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

    Default

    also,
    PHP Code:
    $_COOKIE['Authorization']
    // and
    $_COOKIE['authorization']
    // are _not_ the same cookies 

  8. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Blue walrus, Which page were you reffering to the first second or third. I'm sorry but i don't what the error is that you are pointing out. Could you please try to explain what you meant to me. Thankyou everyone who has helped. Also Traq, I changed the a to an A.

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

    Default

    The error on index.php is

    Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/index.php:12) in /home1/keyboard/public_html/index.php on line 18

    The error on setcookie.php is

    Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/setcookie.php:9) in /home1/keyboard/public_html/setcookie.php on line 12
    The "Cannot modify header information - headers already sent by" means some content has already been outputted so the header can not be used.

    For example

    Valid:

    Code:
    <?php
    if(isset($_COOKIE['lastVisit']))
        header('location: indexthesecond.php'); 
    else
        $visit = $_COOKIE['lastVisit'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>
    Invalid:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <?php
    if(isset($_COOKIE['lastVisit']))
        header('location: indexthesecond.php'); 
    else
        $visit = $_COOKIE['lastVisit'];
    ?> 
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>

    Valid:

    Code:
    <?php
    if(isset($_COOKIE['lastVisit']))
        header('location: indexthesecond.php'); 
    else
        $visit = $_COOKIE['lastVisit'];
    $content = "Blah";
    echo $content;
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>
    Invalid

    Code:
    <?php
    $content = "Blah";
    echo $content;
    if(isset($_COOKIE['lastVisit']))
        header('location: indexthesecond.php'); 
    else
        $visit = $_COOKIE['lastVisit'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>
    This also goes with including files if the file included outputs something it will error the header as well.

    Also, do you mean you changed the a to an A in all other instances or here
    setcookie('Authorization','no', $george);
    ?
    Last edited by bluewalrus; 07-09-2011 at 06:22 PM.
    Corrections to my coding/thoughts welcome.

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

    Default

    I'm sorry, I don't understand. What is the difference between your examples of valid and invalid. Thankyou for helping.

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
  •