会话在PHP中没有变化?

时间:2011-08-07 04:07:01

标签: php session-cookies login-script

帮助!我正在使用Jpmaster77的登录脚本,但需要更改它,因为我需要来自用户和其他东西的更多信息...... 突然间,我遇到了这个问题:

通常,在人员点击“登录”后,脚本会将信息传递给process.php,后者运行session.php。 session.php设置cookie。然后它调用database.php,并提取用户信息。 但是现在,脚本不再转到“登录”页面,而是循环回登录表单。但是,系统已注册我已登录 - 当我运行“谁在线”脚本时,我刚刚登录的帐户显示出来。 我怀疑脚本在session.php部分被卡住了(即遇到错误,或者无法写入数据库或其他东西),但我不确定.....我想检查,但是由于某些奇怪的原因,Xdebug对我不起作用= _ = 所以这是我的代码:

登录表单

<form action="process.php" method="POST" id="user_main">
<span class="label">Username:</span> 
<input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>">       
<? echo $form->error("user"); ?><br />
<span class="label">Password:</span> <input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"><? echo $form->error("pass"); ?><br />
<input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?> />
<span class="label">Remember me next time</span><br />
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"><br /><br />
[<a href="forgotpass.php">Forgot Password?</a>] | Not registered? <a href="register.php">Sign-Up!</a>
</form>

Process.php

(首先检查通过上面文件中的隐藏值发送的表格)

function Process(){
  global $session;
  /* User submitted login form */
  if(isset($_POST['sublogin'])){
     $this->procLogin();
  }

和procLogin说:

function procLogin(){
      global $session, $form;
      /* Login attempt */
      $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
      /* Login successful */
      if($retval){
         header("Location: ".$session->referrer);
      }
      /* Login failed */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }

会话登录()说:

function login($subuser, $subpass, $subremember){
  global $database, $form;  //The database and form object
  /* Username error checking */
  $field = "user";  //Use field name for username
  if(!$subuser || strlen($subuser = trim($subuser)) == 0){
     $form->setError($field, "* Username not entered");
  }
  else{
     /* Check if username is not alphanumeric */
     if(!ctype_alnum($subuser)){
        $form->setError($field, "* Username not alphanumeric");
     }
  }

  /* Password error checking */
  $field = "pass";  //Use field name for password
  if(!$subpass){
     $form->setError($field, "* Password not entered");
  }

  /* Return if form errors exist */
  if($form->num_errors > 0){
     return false;
  }

  /* Checks that username is in database and password is correct */
  $subuser = stripslashes($subuser);
  $result = $database->confirmUserPass($subuser, md5($subpass));

  /* Check error codes */
  if($result == 1){
     $field = "user";
     $form->setError($field, "* Username not found");
  }
  else if($result == 2){
     $field = "pass";
     $form->setError($field, "* Invalid password");
  }

  /* Return if form errors exist */
  if($form->num_errors > 0){
     return false;
  }

  /* Username and password correct, register session variables */
  $this->userinfo  = $database->getUserInfo($subuser);
  $this->username  = $_SESSION['username'] = $this->userinfo['username'];
  $this->userid    = $_SESSION['userid']   = $this->generateRandID();
  $this->userlevel = $this->userinfo['userlevel'];

  /* Insert userid into database and update active users table */
  $database->updateUserField($this->username, "userid", $this->userid);
  $database->addActiveUser($this->username, $this->time);
  $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);

  /**
   * This is the cool part: the user has requested that we remember that
   * he's logged in, so we set two cookies. One to hold his username,
   * and one to hold his random value userid. It expires by the time
   * specified in constants.php. Now, next time he comes to our site, we will
   * log him in automatically, but only if he didn't log out before he left.
   */
  if($subremember){
     setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
     setcookie("cookid",   $this->userid,   time()+COOKIE_EXPIRE, COOKIE_PATH);
  }

  /* Login completed successfully */
  return true;

}

会话开始()

 function startSession(){
  global $database;  //The database connection
  session_start() or die("Line 46");   //Tell PHP to start the session

  /* Determine if user is logged in */
  $this->logged_in = $this->checkLogin();

  /**
   * Set guest value to users not logged in, and update
   * active guests table accordingly.
   */
  if(!$this->logged_in){
     $this->username = $_SESSION['username'] = GUEST_NAME;
     $this->userlevel = GUEST_LEVEL;
     $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  }
  /* Update users last active timestamp */
  else{
     $database->addActiveUser($this->username, $this->time);
  }

  /* Remove inactive visitors from database */
  $database->removeInactiveUsers();
  $database->removeInactiveGuests();

  /* Set referrer page */
  if(isset($_SESSION['url'])){
     $this->referrer = $_SESSION['url'];
  }else{
     $this->referrer = "/";
  }

  /* Set current url */
  $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];

}

CheckLogin()

function checkLogin(){
  global $database;  //The database connection
  /* Check if user has been remembered */
  if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
     $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
     $this->userid   = $_SESSION['userid']   = $_COOKIE['cookid'];
  }

  /* Username and userid have been set and not guest */
  if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
     $_SESSION['username'] != GUEST_NAME){
     /* Confirm that username and userid are valid */
     if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
        /* Variables are incorrect, user not logged in */
        unset($_SESSION['username']);
        unset($_SESSION['userid']);
        return false;
     }

     /* User is logged in, set class variables */
     $this->userinfo  = $database->getUserInfo($_SESSION['username']);
     $this->username  = $this->userinfo['username'];
     $this->userid    = $this->userinfo['userid'];
     $this->userlevel = $this->userinfo['userlevel'];
     return true;
  }
  /* User not logged in */
  else{
     return false;
  }

}

帮助?

0 个答案:

没有答案