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 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2006
    Location
    California. USA
    Posts
    86
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Setting up a cookie Error

    I have the code down below, my error is that when the information is submitted, the cookies are not set. Can anyone see any problem with the script in which it would work?


    PHP Code:
    <?
    $login 
    $_POST['login'];
    if(isset(
    $login)){
    $username strip_tags($_POST['username']);
    $username strtolower($username);
    $username str_replace(" """"$username");
    $username str_replace("'""""$username");
    $username str_replace("\"""""$username");
    $password strip_tags($_POST['password']);
    include(
    "include/database.php");
    $date date("U")-600;
    $query mysql_query("SELECT * FROM users WHERE 

    username='$username' && password='$password' "
    );
    if(
    mysql_num_rows($query) < 1){
    $status "<font color='red'>Invalid Username And/Or Password</font><br 

    />"
    ;
    }else{
    setcookie("sesuser",$username);
    setcookie("sesid",$date);
    $ip $_SERVER['REMOTE_ADDR'];
    $datet date("U");
    mysql_query("DELETE FROM session WHERE username='$username' ");
    $ip $_SERVER['REMOTE_ADDR'];
    $date date("U")+3600;
    mysql_query("INSERT INTO session VALUES('$username', '$date', '$ip', 

    '$password', '$date')"
    );
    $datee date("F jS Y g:iA");
    $datey date("U");
    mysql_Query("UPDATE users SET lastlogon='$datee', lastlogo='$datey' 

    WHERE username='$username' "
    );
    $page $_POST['page'];
    if(isset(
    $page)){
    echo 
    "<script type=\"text/javascript\">
    <!--
    window.location = \"$page\"
    //-->
    </script>"
    ;
    }else{
    ?>
    <script type="text/javascript">
    <!--
    window.location = "member.php"
    //-->
    </script>
    <?
    }}}
    include(
    "include/logheader.php");
    include(
    "include/logininfo.php");
    ?>
    Need a Website?
    http://www.webmaking.biz
    Free Photo Hosting:
    http://www.photophatty.com

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Cookies are set within a header.
    That means you need to either refresh the page with a new header,
    or jump out to another PHP script, set the cookie(s) and jump back.

    With the code you provided, I can't really tell what is happening.

    But you probably have a form where the user puts in a userid and password?

    When that form is submitted, it should jump to a script that verifies the
    login and sets a cookie, then goes to another script using:
    header ("location: index.php");
    ... or whatever page it needs to go to. That motion of using header, sets the cookie.

    Here's the deal though ....
    I question why you're using cookies instead of PHP SESSIONS.
    SESSIONS are much more secure.

    and another thing .... I think your cookies expire if the browser closes
    because you don't specify an expiration date.


  •  

    Posting Permissions

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