dongzhan5286 2013-04-04 03:20
浏览 65
已采纳

login.php没有启动会话或设置数组

I have a login.php file that after a valid login it does not seem to be starting a session or storing any information in the session array? Any help would be appreciated. Thank you.

    <?php 
include_once 'accounts/db.php';
include_once 'accounts/dbfunctions.php';

$err = array();

foreach($_GET as $key => $value) {
    $get[$key] = filter($value); //get variables are filtered.
}

if ($_POST['doLogin']=='Login')
{

foreach($_POST as $key => $value) {
    $data[$key] = filter($value); // post variables are filtered
}


$user_email = $data['usr_email'];
$pass = $data['pwd'];


if (strpos($user_email,'@') === false) {
    $user_cond = "user_name='$user_email'";
} else {
      $user_cond = "user_email='$user_email'";

}


$result = mysql_query("SELECT `id`,`pwd`,`full_name`,`approved`,`user_level` FROM users WHERE 
           $user_cond
            AND `banned` = '0'
            ") or die (mysql_error()); 
$num = mysql_num_rows($result);

  // Match row found with more than 1 results  - the user is authenticated. 
    if ( $num > 0 ) { 

    list($id,$pwd,$full_name,$approved,$user_level) = mysql_fetch_row($result);

    if(!$approved) {
    //$msg = urlencode("Account not activated. Please check your email for activation code");
    $err[] = "Account not activated. Please check your email for activation code";

    //header("Location: login.php?msg=$msg");
     //exit();
     }

        //check against salt
    if ($pwd === PwdHash($pass,substr($pwd,0,9))) { 
    if(empty($err)){            

     // this sets session and logs user in  
       session_start();
       session_regenerate_id(true); //prevent against session fixation attacks.

       // this sets variables in the session 
        $_SESSION['user_id']= $id;  
        $_SESSION['user_name'] = $full_name;
        $_SESSION['user_level'] = $user_level;
        $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);

        //update the timestamp and key for cookie
        $stamp = time();
        $ckey = GenKey();
        mysql_query("update users set `ctime`='$stamp', `ckey` = '$ckey' where id='$id'") or die(mysql_error());

        //set a cookie 

       if(isset($_POST['remember'])){
                  setcookie("user_id", $_SESSION['user_id'], time()+60*60*24*COOKIE_TIME_OUT, "/");
                  setcookie("user_key", sha1($ckey), time()+60*60*24*COOKIE_TIME_OUT, "/");
                  setcookie("user_name",$_SESSION['user_name'], time()+60*60*24*COOKIE_TIME_OUT, "/");
                   }
          header( 'Location: http://www.example.com' ) ;
         }
        }
        else
        {
        //$msg = urlencode("Invalid Login. Please try again with correct user email and password. ");
        //$err[] = "Invalid Login. Please try again with correct user email and password.";
        header("Location: index.html?p=problem1");
        }
    } else {
        header("Location: index.html?p=problem2");
        //$err[] = "Error - Invalid login. No such user exists";
      }     
}



?>
<script type="text/javascript" src="jquery/jquery.validate.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
    $("#logForm").validate();
  });
  </script>
<?php
      /******************** ERROR MESSAGES*************************************************
      This code is to show error messages 
      **************************************************************************/
      if(!empty($err))  {
       echo "<div class=\"msg\">";
      foreach ($err as $e) {
        echo "$e <br>";
        }
      echo "</div>";    
       }
      /******************************* END ********************************/      
      ?>
<div>
<?php
echo "session user id " . $_SESSION['user_id'];
echo "session id" . $sess_user_id;
echo "cookie id" . $cook_user_id;
echo "Session A " . $test1;
Print_r ($_SESSION);
$sid = session_id();
if($sid) {
    echo "Session exists!" . session_id();
} else {
    echo "NOTHING!";
}
echo $_SESSION['user_name'];
echo $_COOKIE['user_id'];
echo $_COOKIE['user_key'];
?>
<a id="11" href="#colorbox" class="Link">Login</a>
</div>
<div style='display:none'>
<div id="colorbox">
  <div id="LoginBox">
    <form action="login.php" method="post" name="logForm" id="logForm">
      <div class="Fields">
      <div id="userName">UserName:</div>
        <input name="usr_email" type="text" class="required" size="25">
      </div>
      <div class="Fields" style="padding-top:5px;">
      <div id="passWord">Password:</div>
        <input name="pwd" type="password" class="required password" size="25">
      </div>
      <div class="Fields" style="padding-top:5px;">
        <input name="remember" type="checkbox" id="remember" value="1">
        Remember me</div>
      <div class="Fields" style="padding-top:5px;text-align:center;">
        <input name="doLogin" type="submit" id="doLogin3" class="button" value="Login">
      </div>
    </form>
    <div id="forgotPasswordLink">
    <a class="sitelink" id="colorboxForgot" href="forgot.php">Forgot Password</a></div>
  </div>
</div>
</div>

Right now after login I see url index.html?p=problem instead of http://www.example.com

the following echos are empty so I believe that means no information is going into the session array

<?php
echo "session user id " . $_SESSION['user_id'];
echo "session id" . $sess_user_id;
echo "cookie id" . $cook_user_id;
echo "Session A " . $test1;
Print_r ($_SESSION);
$sid = session_id();
if($sid) {
    echo "Session exists!" . session_id();
} else {
    echo "NOTHING!";
}
echo $_SESSION['user_name'];
echo $_COOKIE['user_id'];
echo $_COOKIE['user_key'];
?>
  • 写回答

1条回答 默认 最新

  • dongyan9838 2013-04-04 03:42
    关注

    keep in mind that you should start session at the very top of thd page,ie before anything start.else you will get empty values for session variables. if you wish to use session variables.Move session_start();

    to fist line.

    sure this will solve ur problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值