SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Cookies

  1. #1
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookies

    Hi Guys,

    I am just starting to have a mess with cookies, and have the following on a page where it sets one:

    if ($_POST['userbox'] == "Mike" AND $_POST['passbox'] == "Ace" )
    {
    // register the 'loggedon' session.
    setcookie ("loggedon", "", time()+3600);
    $loggedon = "yes";
    ?>

    Just to test to see if they worked I made a page in another folder and put this in:

    <?php
    echo $_COOKIE["loggedon"];
    ?>

    I was expecting it to say 'yes' when I went on the page with the above three lines of code, but instead it said nothing. Just presented me with a white page.

    Does anyone know whats the problem please?

    Cheers,
    Mike

    PS - I went on the page it set the cookie and the code isn't in code format as it doesn't seem to be working...
    Last edited by webmasternovis; Mar 29, 2003 at 05:09.

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    test... 
    Code:
    this board is messed up...
    mm, php or code doesn't seem to be working
    Last edited by website; Mar 30, 2003 at 11:21.
    - website

  3. #3
    SitePoint Addict sleepingdanny's Avatar
    Join Date
    Oct 2002
    Location
    Israel
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this code - it should work =>

    <?
    if ($_POST['userbox'] == "Mike" AND $_POST['passbox'] == "Ace" )
    {
    setcookie ("loggedon", "", time()+3600);
    }
    ?>

    Then in a certein page put this code above the <html> tag =>

    <?
    if(!isset($_COOKIE['loggedon'])){ //if not logged in
    header("location: http://.../notlogged.php" );
    }
    ?>
    <html>
    <head>
    </head>
    <body>
    Here you need the html content if the user is logged in...
    </body>
    </html>
    Last edited by sleepingdanny; Mar 29, 2003 at 06:19.

  4. #4
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    Sorry, still don't work. Here are both sets of my code:


    <?php
    if ($_POST['userbox'] == "Mike" AND $_POST['passbox'] == "Ace" )
    {
    setcookie ("loggedon", "", time()+3600);
    ?>
    <html>
    <head>
    <style type="text/css">
    <!--
    .box
    {
    border-width:1px;
    border-color:#808080;
    border-style:solid;
    height:200px;
    width:300px;
    }
    -->
    </style>
    </head>
    <body>
    <?php
    // display a success message. For testing purposes, link to the 'private page'
    echo "<body bgcolor='#E0E0E0' link='#999999'>";
    echo "<p align='center'><br>";
    echo "<br><br><br></p><div align='center'><center><table border='1' width='47%' height='67' bordercolor='#E0E0E0' bordercolorlight='#E0E0E0' bordercolordark='#E0E0E0'><tr><td width='100%' height='61'><div class='box' style='width: 316; height: 111'><p align='center'><br>";
    echo "<b><font size='2' face='Verdana' color='#999999'>";
    echo "You have successfully logged in as {$_POST['userbox']}";
    echo "<br>";
    echo "<br>";
    echo "Click <a href=""\"
    http://www.gawmir.com/members/loggedon.php\">here</a> to complete the login process and enter the members area.</font></b>";
    echo "</div></td></tr></table></center></div>";
    }

    ?>

    And the one where its supposed to say weather your logged in or not:

    <?php
    if(!isset($_COOKIE['loggedon'])){ //if not logged in
    echo "logged in";
    }
    else {
    ?>
    <html>
    <head>
    </head>
    <body>
    Here you need the html content if the user is logged in...
    </body>
    </html>
    <?php
    }

    ?>


    Please could someone help?

  5. #5
    SitePoint Addict sleepingdanny's Avatar
    Join Date
    Oct 2002
    Location
    Israel
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that I fixed your code, try it now images/smilies/tongue.gif

    <html>
    <head>
    <style type="text/css">
    <!--
    .box
    {
    border-width:1px;
    border-color:#808080;
    border-style:solid;
    height:200px;
    width:300px;
    }
    -->
    </style>
    </head>
    <body>

    <form method="POST" action="">
    Username: <input type="text" name="userbox"><br>
    Password: <input type="password" name="passbox"><br>
    <input type="submit" value="Login!">
    </form>

    <?php
    if($userbox=="Mike" && $passbox=="Ace" ){
    setcookie("loggedon", "", time()+3600); //cookie for an hour

    }

    elseif(isset($_COOKIE['loggedon'])){
    echo "<body bgcolor='#E0E0E0' link='#999999'>";
    echo "<p align='center'><br>";
    echo "<br><br><br></p><div align='center'><center><table border='1' width='47%' height='67' bordercolor='#E0E0E0' bordercolorlight='#E0E0E0' bordercolordark='#E0E0E0'><tr><td width='100%' height='61'><div class='box' style='width: 316; height: 111'><p align='center'><br>";
    echo "<b><font size='2' face='Verdana' color='#999999'>";
    echo "You have successfully logged in as $userbox'";
    echo "<br>";
    echo "<br>";
    echo "Click ...
    to complete the login process and enter the members area.</font></b>";
    echo "</div></td></tr></table></center></div>";
    }

    else{
    echo "Not logged in yet!";
    }
    ?>
    Last edited by sleepingdanny; Mar 30, 2003 at 05:19.

  6. #6
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm....

    There are no errors or anything, but it doesn't look like the cookie has been set. I leave the page, and then go back to it and it says, "Not logged in yet!".

    Know whats wrong please?

  7. #7
    SitePoint Addict sleepingdanny's Avatar
    Join Date
    Oct 2002
    Location
    Israel
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webmasternovis
    Hmmm....

    There are no errors or anything, but it doesn't look like the cookie has been set. I leave the page, and then go back to it and it says, "Not logged in yet!".

    Know whats wrong please?
    I think I got an idea, try this - it works for me

    <html>
    <head>
    <style type="text/css">
    <!--
    .box
    {
    border-width:1px;
    border-color:#808080;
    border-style:solid;
    height:200px;
    width:300px;
    }
    -->
    </style>
    </head>
    <body>
    <?php
    if($userbox=="Mike" && $passbox=="Ace" ){
    setcookie("loggedon", "", time()+3600) && header("location: http://.../members.php" ); // page if logged in
    }

    ?>
    <form method="POST" action="">
    Username: <input type="text" name="userbox"><br>

    Password: <input type="password" name="passbox"><br>
    <input type="submit" value="Login!">
    </form>
    </body>
    </html>

    Then in the members page put this code at the top of the <html> tag

    <?php
    if(!isset($_COOKIE['loggedon'])){ // if cookie isn't set
    header("location: http://.../forbiden.php" ); // page if not logged in
    }
    ?>
    <html>
    <body>
    MEMBERS AREA CONTENT
    </body>
    </html>

    The members area content will be shown only if the cookie is set!!!
    If cookie isn't set the user will be sent to "forbiden.php" that you created
    Last edited by sleepingdanny; Mar 31, 2003 at 01:28.

  8. #8
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... I don't really get you, but....

    I have the index file:

    <?php
    if ($userbox=="Mike" AND $passbox=="Ace" ){
    setcookie("loggedon", "", time()+3600) && header("location:
    http://www.gawmir.com/members/members.php" ); // page if logged in
    }
    ?>
    <html>
    <body>
    <form method="POST" action="members.php">
    Username: <input type="text" name="userbox"><br>
    Password: <input type="password" name="passbox"><br>
    <input type="submit" value="Login!">
    </form>
    </body>
    </html>


    Which I don't get as why would you have 'if ($userbox=="Mike AND $passbox=="Ace )' before the actual form?

    members.php is as follows:

    <?php
    if (!isset($_COOKIE['loggedon'])){ // if cookie isn't set
    header("location:
    http://www.gawmir.com/members/forbiden.php" ); // page if not logged in
    }
    ?>
    <html>
    <body>
    MEMBERS AREA CONTENT
    </body>
    </html>


    and forbiden.php is:

    <?php
    echo "you are not logged in";
    ?>

    All of that doesn't work, I think I have split it up into the sections you said - but, should that work?
    Last edited by webmasternovis; Mar 31, 2003 at 09:16.

  9. #9
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> setcookie("loggedon", "", time()+3600)

    Aren't you setting the cookie there to have a blank value? Try

    setcookie("loggedon", "1", time()+3600)
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  10. #10
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mate, works Also thanks sleepingdanny for all the help you have given me

    Bye the way Overgrow (or anyone), what happens if you give the cookie a higher/lower value please?

  11. #11
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing. Since you're checking to see if it exists, and not to see if it has a specific value, it doesn't matter what you set it to.

    setcookie("loggedon", "hansblix", time()+3600)
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  12. #12
    Wishful Thinking webmasternovis's Avatar
    Join Date
    Jul 2002
    Location
    England
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks mate, and thanks again to sleepingdanny


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
  •