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 13 of 13

Thread: cookies

  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cookies

    hi, i have this log in script:

    PHP Code:
    <?php


    include 'config.php';

    ob_start();
    echo 
    "<font size=\"2\" face=\"verdana\"> Login </font><br>";
    echo 
    "<form action=\"./login.php\" method=\"POST\">";
    echo 
    "<font size=\"1\" face=\"verdana\">Name:</font> <br><input type=\"text\" name=\"username\"><br>";
    echo 
    "<font size=\"1\" face=\"verdana\">Your email:</font> <br><input type=\"text\" name=\"email\"><br>";
    echo 
    "<font size=\"1\" face=\"verdana\">Pass: </font><br><input type=\"password\" name=\"password\"><br>";
    echo 
    "<input type=\"submit\" value=\"Login!\">";
    echo 
    "</form>";
    echo 
    "<font size=\"1\" face=\"verdana\">Forgot your password?<br> Click <a href=\"reset.php\">here!</font></a>";

    $connection = @mysql_connect($hostname$user$pass)
    or die(
    mysql_error());
    $dbs = @mysql_select_db($database$connection) or
    die(
    mysql_error());

    $sql "SELECT * FROM `users` WHERE username = '$_POST[username]' AND password = '$_POST[password]' AND email = '$_POST[email]'";
    $result = @mysql_query($sql,$connection) or die(mysql_error());
    $num = @mysql_num_rows($result);

    if (
    $num != 0) {
    $cookie_name "auth";
    $cookie_value "fook";
    $cookie_expire "0";
    $cookie_domain $domain;

    setcookie($cookie_name$cookie_value$cookie_expire"/"$cookie_domain0);
    header ("Location: http://" $domain  $directory "index1.php");

    ob_end_flush();

    exit;
    }
    ?>
    can someone help me to add the username of the user to the cookie that is created and then advise me of the code i can use on another page to retrive the username set in the cookie to enable me to query the database...

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm a bit confused.

    do you wanna replace the "fook" by the username? or does that values needs to be kept as well?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that value can be changed to the username. how would i do that and how would i get that from my page that you see when logged in?

  • #4
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the top of my protected page looks like this if that helps: (think the fook might need to stay...

    PHP Code:
    <?php
    include 'config.php';
    $tmp $_GET['action'];
    if(
    $tmp == "signout"){
    $cookie_name "auth";
    $cookie_value "";
    $cookie_expire "0";
    $cookie_domain $domain;
    setcookie($cookie_name$cookie_value$cookie_expire"/"$cookie_domain0);
    header ("Location: http://" $domain $directory "index.php");
    }

    if(
    $_COOKIE['auth'] == "fook"){
    echo 
    "";

    echo ;
    }else{
    header ("Location: http://" $domain $directory "index.php");
    exit;
    }
    ?>

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just put username in cookie and after that you can check whether that cookie exist.
    If you can not find a decision, maybe you have to try to change a problem

    http://www.gunman.co.nr
    http://bglinux.org

  • #6
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that made no sense to me what so ever. can someone explain what i need to do. i am really new to php so not exactly sure what im doing.!

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, if that 'fook' needs to remain the value of cookie 'auth', then i think it would be best to just set a second cookie.
    (you can use one cookie as an array of values, but that would still mean you need to check and maybe adjust your other pages that are now using that cookie.)

    so, to set the extra cookie, it would be as simple as
    PHP Code:
    //your existing cookie being set
    setcookie($cookie_name$cookie_value$cookie_expire"/"$cookie_domain0);
    //set extra cookie
    setcookie('uname'$_POST[username], $cookie_expire"/"$cookie_domain0); 
    and to read the value on other pages, it's just
    PHP Code:
    if (isset($_COOKIE['uname'])){
        
    $username $_COOKIE['uname'];

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi raf,

    i have added in the code for the login page creating the cookie and then i have added in this line of code into the page that i want to use the cookie info:

    PHP Code:
    if (isset($_COOKIE['username'])){ 
        
    $username $_COOKIE['username']; 
    $query "SELECT * FROM users WHERE username='" mysql_real_escape_string($_COOKIE['username']) . "'"
    $result mysql_query($query) or die("Mysql Error: ".mysql_error); 

    while(
    $row mysql_fetch_array($result)){ 

    ?> 

    This doesnt appear to work! any ideas?

  • #9
    New Coder
    Join Date
    Sep 2005
    Location
    Berlin, Germany
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is the Cookie set?
    try that:
    PHP Code:
    echo '<pre>';
    var_dump($_COOKIE);
    echo 
    '</pre>'
    and: don't forget to reload the page after setting the cookie - only than you'll be able to see if it is set or not.

  • #10
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    am i meant to be placing that in a single php file with no other code?

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "This doesnt appear to work!" as not much to go on ...

    i do however see that you have
    if (isset($_COOKIE['username'])){

    --> did you rename the cookie in the code i posted to set it ?

    also, inside your query, use $username instead of $_COOKIE['username']

    if it doesn't work, then add in some echo's at strategical points. Like
    PHP Code:
    if (isset($_COOKIE['username'])){ 
       echo 
    '1<br />'
       
    $username $_COOKIE['username']; 
    echo 
    $username;
    $query "SELECT * FROM users WHERE username='" mysql_real_escape_string($_COOKIE['username']) . "'"
    echo 
    '<br />'$query;
    $result mysql_query($query) or die("Mysql Error: ".mysql_error); 
    echo 
    '2<br />';
    while(
    $row mysql_fetch_array($result)){ 
    and then look at the results to follow the executionpath.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #12
    New Coder
    Join Date
    Sep 2005
    Location
    Berlin, Germany
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by robojob
    am i meant to be placing that in a single php file with no other code?
    that code is meant to be placed inside your existing php-file. I would say just before the if(isset())

  • #13
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    k, its working now! i noticed a mistake that i made somewhere else in the code!


  •  

    Posting Permissions

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