Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    $_COOKIE Need Help!!!


    I am using PHP5. I would like to get the $_COOKIE variable to work, so I could implement a "remember me" feature. For some reason my setcookie function does not work.

    setcookie( "username", "hello", time()+60*60*24 );
    setcookie( "password", "hey", time()+60*60*24 );

    I am putting this function at the top of my page and still no luck. If it did work all I would do is check

    isset( $_COOKIE[ "username" ] );
    isset( $_COOKIE[ "password" ] );

    and then redirect

    header( "Location: getin.php" );
    exit

    and that would be it. How can I get the setcookie function to work. Do I have enable in the php.ini file. Is it deprecated. Should I use $_REQUEST[ ] really weird. Someone please help. Am I missing a line of code somewhere?

    Thank you,

    Val
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Keep in mind


    Keep in mind this is just pseudocode. I gets interpreted fine, but the setcookie function is disregarded.

    Val
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    32
    Rep Power
    10
    In remember me functions, storing the username and password on the client's computer is a very bad idea as far as securty goes.

    Remember me functions are hard to code as a balance between security and them actually working, I'm still working on one for my own site.

    Someone else can probably help more, sorry.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Actually


    If the user chose to be remembered then when they close and reopen their browser they will still be redirected as if they logged in. Sessions are lost as soon as the user closes their browser, but cookies should persist. I know that the user could disable cookies on their machine, but I don't care. Cookies are enabled on my machine, and I still can't get it to work. Actually that is what I really need. Even if the user closes their browser, and reopens it they will be redirected is there something wrong with my PHP.ini configuration. Someone please help!!!!!!!!!!!

    Val
  8. #5
  9. Web Programmer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    362
    Rep Power
    13
    You need to reload (forward) the page after setting the cookie but before trying to read the cookie. You may be doing this already but since you posted pseudo code it is hard to tell.
    Matt Light
    Web Programmer
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Croatia
    Posts
    630
    Rep Power
    0
    well your code should work. i use cookie for remembering my users and i don't have any problem.

    try printing cookie array as soon as you set cookies to see what dose it print:
    PHP Code:
    setcookie"username""hello"time()+60*60*24 );
    setcookie"password""hey"time()+60*60*24 );
    print_r($_COOKIE); 
  12. #7
  13. Web Programmer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    362
    Rep Power
    13
    That should not print anything. As my previous post should have conveyed, cookies are not available to be read until after the next loading of a page.
    Originally Posted by PHP Manual
    Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
    Matt Light
    Web Programmer
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Croatia
    Posts
    630
    Rep Power
    0
    Originally Posted by Matt Light
    That should not print anything. As my previous post should have conveyed, cookies are not available to be read until after the next loading of a page.
    of course. but as i understood his post, he is having trouble with cookies in general. to see if they are set use what i posted and refresh the page. if setting of a cookie went well you should see it.

    to valensnow: when you writte code for autologin using cookies and session you should first check if user has cookies set (check that content is valid allso). if he dose, than redirect him.

    some time ago i have posted my login script here where i use cookie to autologin user, so you can take a look at it
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Hey Man


    I have already tried doing a small example and it works, but for some reason this one won't. I have an include file above the setcookie which will have markup this would cause it not to work, but then when I comment it out it still does not work.
    Don't know what to do. Post a message back saying if you are still available I will send you part of my code directly. It will take some time to cut and paste it out to temp files. Let me know if you are available. Otherwise, I'll just keep trying. It seems like you know what you are doing based on your code. Let me know.

    Thanks.

    Val

    Originally Posted by zvoneM
    of course. but as i understood his post, he is having trouble with cookies in general. to see if they are set use what i posted and refresh the page. if setting of a cookie went well you should see it.

    to valensnow: when you writte code for autologin using cookies and session you should first check if user has cookies set (check that content is valid allso). if he dose, than redirect him.

    some time ago i have posted my login script here where i use cookie to autologin user, so you can take a look at it
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Croatia
    Posts
    630
    Rep Power
    0
    shure, just post your code and i (actually we) will see what can be done. but before that i would recomend you to take a look at my code. it can serve you as reference (it should work just by copy/paste) and maybe you will get to solution to your problem. allways is better to solve something by your own than by getting quick-fix solutions from a side
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Let me put some sample code on here


    Here is some sample code that models my idea. I am so retarded, I don't even understand why this does not work, but I know you will. It's great to make friends. Much love peoples.

    Here is the idea. There are three small pages.

    The first page (first.php):

    <?php
    if( isset( $_COOKIE[ "username" ] ) )
    {
    header( "Location: final.php" );
    exit;
    }
    ?>
    <html>
    <body>
    <form name="user" method="post" action="next.php">
    <input type="text" name="username" size="30"/>
    <input type="checkbox" name="remember"/>
    <input type="submit" value="Submit"/>
    </form>
    </body>
    </html>

    The second page (next.php):

    <?php

    $username = trim( $_POST[ "username" ] );

    if( $username != "" )
    {
    if( isset( $_POST[ "remember" ] ) ){
    setcookie( "username", $GLOBALS[ "username" ], mktime()+(86400*30), "/" );
    } // Cookie does not get set.
    header( "Location: final.php" );
    exit;
    }
    }
    ?>


    The third page (final.php):

    <html>
    <head>
    <title>How are you?</title>
    </head>
    <body>
    <a href="first.php">See ya</a>
    </body>
    </html>

    The first page leads to the next page which checks if the username is empty. If it is not it will take the user to the final page. If the checkbox "remember" was checked on the first page it should setcookie on the second page (next.php).
    Assuming the user checked the "remember" checkbox on the first page if the user "CLOSES" their browser completely and reopens it they should be taken to the final.php page. BUT they are NOT!!! Why??? I know COOKIES work on my machine.

    The tiny test script below works just fine.

    <?php

    if (!isset($_COOKIE['visited'])) {
    // if a cookie does not exist
    // set it
    setcookie("visited", "1", mktime()+86400, "/") or die("Could not set cookie");
    echo "This is your first visit here today.";
    }
    else {
    // if a cookie already exists
    echo "Nice to see you again, old friend!";
    }

    ?>

    Why does mine not work. All I want is for the user to be redirected to the final.php page if they close and reopen their browser, but for some reason the cookie is not set. This is a tiny theoretical case that I created just to test my huge case and as tiny as it is it also does not work. All I want is for when the user opens the first page if a cookie was set redirect them to the last page. Perhaps I will have to add additional code for the other two pages. But let's get this to work first.

    Thanks.

    Val
  22. #12
  23. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Croatia
    Posts
    630
    Rep Power
    0
    ok. this is one way to go. i have joined login form and script that redirects you to other page in one since there is no need for two separate pages. plus i added session to block direct access to members.php, so you got to login to access that page. this should get you started. but don't use this as it is since it lacks couple of basic things - validation of post and cookie variables. plus, if you are going to set password in cookie than make it shure that it is encrypted in one way encryption (md5, sh1 or what you want). same thing is for session. you don't need to set password in session, but other info should be encrypted and you should regenerate session sid on every page. ok here is the code:

    login.php:
    PHP Code:
    <?php
    session_start
    ();
    function 
    loginForm(){
        
    ?>
            <html>
                <head>
                    <title>Login page</title>
                </head>
                <body>
                    <p>Please login to enter members page</p>
                    <form name="user" method="post" action="login.php">
                        <input type="text" name="username" size="30"/>
                        <input type="checkbox" name="remember"/>
                        <input type="submit" value="Submit"/>
                    </form>
                </body>
            </html>
        <?php
    }
    if(isset(
    $_SESSION['username'])){
        
    header('Location: members.php');
        exit;
    }
    elseif(isset(
    $_COOKIE['username'])){
        
    $_SESSION['username'] = $_COOKIE['username'];
        
    header('Location: members.php');
        exit;
    }
    elseif(
    $_POST['username'] != ''){
        if(
    $_POST['remember'] == 'on'){
            
    setcookie('username'$_POST['username'], mktime()+(86400*30));
        }
        
    $_SESSION['username'] = $_POST['remember'];
        
    header('Location: members.php');
        exit;
    }
    else{
        
    loginForm();
    }
    ?>
    members.php:
    PHP Code:
    <?php
        session_start
    ();
        if(isset(
    $_SESSION['username'])){
            
    ?>
                <html>
                    <head>
                        <title>How are you?</title>
                    </head>
                    <body>
                        <a href="login.php">See ya <?php echo $_COOKIE['username']; ?></a>
                    </body>
                </html>
            <?php
        
    }
        else{
            exit(
    'Please <a href="login.php">login</a> to access members page');
        }
    ?>

    Comments on this post

    • valensnow agrees
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Not Bad


    Hey thanks for taking the time to reply. Bless you. The second page that I have is actually where the validation of the $_POST stuff takes place and that is where the cookie gets set (setcookie). Then it directs them to the final.php which in your case is members. The first page is the login page you guessed it, but if the cookie was set on the second page which it is not (THAT IS THE WHOLE PROBLEM) then it redirects them to the final.php page. So, it goes like this with my three pages. The third page or members page will have a logout script. No problem. Already done.

    1 => 2 => 3 No cookie set validation went thru

    1 => 2 => 1 No cookie set validation did not go thru

    1 => 3 Cookie set go directly to members page

    3 => 1 Logout, cookie and session stuff destroyed

    How can we get it to work with the three pages that I have now and why isn't the cookie getting set in the second page based on the tiny 3 page example that I posted earlier.

    Thanks for everything.

    Val

    Originally Posted by zvoneM
    ok. this is one way to go. i have joined login form and script that redirects you to other page in one since there is no need for two separate pages. plus i added session to block direct access to members.php, so you got to login to access that page. this should get you started. but don't use this as it is since it lacks couple of basic things - validation of post and cookie variables. plus, if you are going to set password in cookie than make it shure that it is encrypted in one way encryption (md5, sh1 or what you want). same thing is for session. you don't need to set password in session, but other info should be encrypted and you should regenerate session sid on every page. ok here is the code:

    login.php:
    PHP Code:
    <?php
    session_start
    ();
    function 
    loginForm(){
        
    ?>
            <html>
                <head>
                    <title>Login page</title>
                </head>
                <body>
                    <p>Please login to enter members page</p>
                    <form name="user" method="post" action="login.php">
                        <input type="text" name="username" size="30"/>
                        <input type="checkbox" name="remember"/>
                        <input type="submit" value="Submit"/>
                    </form>
                </body>
            </html>
        <?php
    }
    if(isset(
    $_SESSION['username'])){
        
    header('Location: members.php');
        exit;
    }
    elseif(isset(
    $_COOKIE['username'])){
        
    $_SESSION['username'] = $_COOKIE['username'];
        
    header('Location: members.php');
        exit;
    }
    elseif(
    $_POST['username'] != ''){
        if(
    $_POST['remember'] == 'on'){
            
    setcookie('username'$_POST['username'], mktime()+(86400*30));
        }
        
    $_SESSION['username'] = $_POST['remember'];
        
    header('Location: members.php');
        exit;
    }
    else{
        
    loginForm();
    }
    ?>
    members.php:
    PHP Code:
    <?php
        session_start
    ();
        if(isset(
    $_SESSION['username'])){
            
    ?>
                <html>
                    <head>
                        <title>How are you?</title>
                    </head>
                    <body>
                        <a href="login.php">See ya <?php echo $_COOKIE['username']; ?></a>
                    </body>
                </html>
            <?php
        
    }
        else{
            exit(
    'Please <a href="login.php">login</a> to access members page');
        }
    ?>
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    203
    Rep Power
    12

    Oh yeah


    The whole idea behind this cookie is that I am implementing a remember me feature. Dah.

    Val
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    16
    Rep Power
    0
    If they clear that cookies, won't that render you remember me feature useless anyway.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo