Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2

Thread: Cookie Problems

  1. #1
    Member
    Join Date
    Oct 2002
    Posts
    32
    Member #
    249
    Hi,

    I've developed a rather simple login system. Here's the gist of it:

    A user goes to the page and goes to the login.php page. They type their user name and password, and if they are correct, then a cookie is set and they become logged in. The action on the form has them go to a different page. This page is located at /login.php.

    Now for the "member's" section. The index page of that checks to see if they are logged in and if so, let them see the content and if not, then redirect them back to the login.php page. This page is located at /members/index.php. I'm having a hard time getting this to work. It just keeps redirecting me to the login page, however if I take the cookie bit out it lets me see the content.

    The purpose of using cookies is so that users don't have to login everytime they want to visit the site, they only have to do it once. The code I have is below for the respective pages. Thank you for any help you can provide.

    PHP Code:
    // This is the login.php page
            <?php
          
    if(empty($_POST['uname']) OR empty($_POST['passwd'])) {
              
    ?>
            <form name="memberlogin" id="memloginform" method="post" action="/ilog/members/index.php">
              <p align="center"><font color="#696969" size="-1" face="Tahoma">User 
                name:</font> 
                <input type="text" name="uname" class="txtfield" />
              </p>
              <p align="center"><font color="#696969" size="-1">Password:</font>&nbsp;&nbsp; 
                <input type="password" name="passwd" class="txtfield" />
              </p>
              <p align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                <input type="submit" name="submit" value="Login" class="btnsubmit" />
              </p>
            </form>
            <?php
          
    }
          if(
    $_POST['submit']) {
          
    $login mysql_query("SELECT * FROM members WHERE `username` = '{$_POST['$uname']}' AND `password` = '" .md5($_POST['passwd'])."'");
          
    $num mysql_num_rows($login);
          if(
    $num 0) {
          
    setcookie("user[]"$uname);
          
    setcookie("pass[]"$passwd);
              
    ?>
            <form name="memberlogin" id="memloginform" method="post" action="/ilog/members/index.php">
              <p align="center"><font color="#696969" size="-1" face="Tahoma">User 
                name:</font> 
                <input type="text" name="uname" class="txtfield" />
              </p>
              <p align="center"><font color="#696969" size="-1">Password:</font>&nbsp;&nbsp; 
                <input type="password" name="passwd" class="txtfield" />
              </p>
              <p align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                <input type="submit" name="submit" value="Login" class="btnsubmit" />
              </p>
            </form>
            <div align="center"><font color="#696969" size="-1" face="Tahoma"><b>Invalid 
              user name and/or password. Please try again.</b></font> 
              <?php
          
    }
          }
          
    ?>

    // This is the /members/index.php page.
    // The very first line of the page is this:

    <?php
    if(!isset($HTTP_COOKIE_VARS["user"])) {
    header("Location: /ilog/login.php");
    }
    ?>

    // The rest of the content is below this and is just HTML.

  2.  

  3. #2
    Member Black Vivi's Avatar
    Join Date
    Apr 2003
    Location
    UK
    Posts
    75
    Member #
    1072
    try setting how long the cookie is stored for, to make it for a year, use this:

    PHP Code:
    setcookie("user[]"$uname,time()+(3600*24*365));
    setcookie("pass[]"$passwd,time()+(3600*24*365)); 
    Download Firebird now!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:24 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com