Making A Cookie

Discussion in 'PHP' started by KGkiller, Dec 10, 2007.

  1. #1
    Hello i made a website with a database and all i got the login to work but when i refresh it logs me back out my friend told me i need to get a cookie but i dont no how to make one i just want when the user logs in he stays on for 1 hour can any one help me please Thanks Ryan
     
    KGkiller, Dec 10, 2007 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
  3. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    setcookie("logged_in", 1, time()+3600);
    
    PHP:
    Then youll have to check that the cookie exists. This isnt fool proof and can be easily hacked, but it will work :)
     
    tonybogs, Dec 10, 2007 IP
  4. golddragon

    golddragon Active Member

    Messages:
    583
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #4
    golddragon, Dec 10, 2007 IP
  5. KGkiller

    KGkiller Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i put that in the notepad and saved it as cookie.php and it didnt work i put it in the htdocs folder and all
     
    KGkiller, Dec 11, 2007 IP
  6. theadud

    theadud Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It important that the cookiecode comes before the <HTML> tag ;)
    Example in how it's done;
    this should be in the "login.php"
    
    <?php
    if($username==$getusername)
    setcookie("logged_in", 1, time()+3600);
    ?>
    
    PHP:
    This should be in all the pages that you need to login to get to;
    
    <?php
    if($_COOKIE["logged_in"])
    {
    //here you put all the site-code
    }
    else
    echo "You are not logged in";
    ?>
    
    PHP:
    hope it works ;D

    Tip:

    you could do like this;
    
    <?php
    if($_COOKIE["logged_in"])
    {
    ?>
    <!--- here you put all the site-html and you don't need the "echo" --->
    <?php
    }
    else
    echo "You are not logged in";
    ?>
    PHP:
     
    theadud, Dec 11, 2007 IP
  7. KGkiller

    KGkiller Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That shows it so much better thank you im going to try it right now
     
    KGkiller, Dec 12, 2007 IP
  8. KGkiller

    KGkiller Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it still does not work what i have is 2 iframes 1 on the left and 1 on the right the right 1 is logged in but the left 1 asks me to keep logging in because when u go to a page and you r not logged in it will bring u back to the login page if you want to look for yourself go to http://71.196.111.196 and the login name is test the password is test123 the right iframe will say error 404 page not found because i didnt make that page yet :p but the thing i need to work is the left iframe wich is members.php and i dont no why it wont work it worked fine when i was using abyss webserver and then i switched to apache and it didnt work right please help thanks :D
     
    KGkiller, Dec 12, 2007 IP
  9. KGkiller

    KGkiller Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    bump bump bump
     
    KGkiller, Dec 13, 2007 IP