Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    51
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Cookies trouble...

    Hi...
    I start to learn cookies.
    First, what I have learn is by writing these code:

    PHP Code:
    <?php 
    setcookie
    ("uname"$nametime()+3600);
    ?>
    <html>
    <body>
    <p>
    A cookie was set on this page! The cookie will be active when 
    the client has sent the cookie back to the server.
    </p>
    </body>
    </html>
    PHP Code:
    <html>
    <body>
    <?php
    if (isset($_COOKIE["uname"]))
    echo 
    "Welcome " $_COOKIE["uname"] . "!<br />";
    else
    echo 
    "You are not logged in!<br />";
    ?>
    </body>
    </html>
    But, it always display "You are not logged in!".

    Help me please...
    Last edited by redfroc; 11-30-2006 at 12:49 PM.

  • #2
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it to work on mine...here's my code:
    PHP Code:
    //At beginning of page
    <?php 
    if (isset($name)){
    setcookie("uname"$nametime()+3600);
    }
    ?>
    //In body of page...
    <?php
    if (isset($_COOKIE["uname"])) {
    echo 
    "Welcome " $_COOKIE["uname"] . "!<br />";
    }
    else {
    echo 
    "You are not logged in!<br />";
    }
    ?>
    <form name="login" action="<?php print $PHP_SELF?>" method="post">
    <input type="text" name="name" />
    <input type="submit" value="submit" />
    </form>
    Yours may not all happen on the same page. But, the code should be similar.

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    51
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks.. it works now..
    and how about the session? where's should I put the session syntax, so as long as user is logging in, the current page always active.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Just put session_start() at the top of any of your pages you want the session to be accessible from. Do not use cookies to track a user, sesions are always a better idea, especially if you designed your system. Then just use $_SESSION to get and set your session variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean so that each page loaded "realizes" the user is still logged in?

    Hmmm, I don't have a lot of experience with sessions. You can use cookies and sessions separately.

    What I would do personally, given my limited knowledge of sessions, is to just include the issset syntax at the beginning of each page to determine if that cookie is active.

    I have an include file that has all sorts of oft-used functions in it. So, I would put the code in there since I always have that file included on each page.

    Sorry, I'm not much help on this part of it.

    Fear not, someone with sessions experience can help you.

    See, it happened already! That's why sessions were included in PHP after cookies...sessions are better!

  • #6
    New Coder
    Join Date
    Mar 2006
    Posts
    51
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have learn about session at php helps. but it can't help me so much.
    I still don't understand completely.
    May somebody could give an example to do with that.
    I need some code to track user as long as logging in and page would be unavailable when user has logged out.

    please help me...


  •  

    Posting Permissions

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