【问题标题】:$_SESSION passing but displays nothing?$_SESSION 通过但什么也不显示?
【发布时间】:2015-07-22 09:44:33
【问题描述】:

我已阅读与此相关的其他问题,但它没有回答我的问题或产生任何故障排除结果。我有一个登录页面。它运行良好,它正在启动会话并且不显示任何内容。即使我放了一个回声“Hello World”;什么都没有显示。它仍然显示在 login.php 而不是 products.php 中。

代码如下: 会话开始(session_start函数前没有空格:

<?php
session_start();
/* connection info */
include("connection.php");
?>

检查空白字段:

if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
  {
    foreach($_POST as $field => $value)         
    {     
        if(empty($value))
          {
             $blank_array[] = $field;
          }
    else                                                    
    {
      $good_data[$field] = strip_tags(trim($value));
    }
  } 

  if(@sizeof($blank_array) > 0) 
  {
    $message = "<p style='color: red; margin-bottom: 0; 
                 font-weight: bold'>
                 You didn't fill in one or more required fields. 
                 You must enter: 
                 <ul style='color: red; margin-top: 0; 
                 list-style: none' >";

 /* display list of missing information */
    foreach($blank_array as $value)
    {
       $message .= "<li>$value</li>";
    }
    $message .= "</ul>"; 

    echo $message;
    extract($good_data);
    include("login.inc");   
    exit();    
  }

检查格式一致性:

foreach($_POST as $field => $value)
{
  if(!empty($value))
  {
    $user_patt = "/^[A-Za-z0-9]{3,20}$/";
    $pass_patt = "/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,12}$/";

    if(preg_match("/user/i",$field))
    {
      if(!preg_match($user_patt,$value))
      {
        $error_array[] = "$value is an invalid $field";
      }
    }

    if (preg_match("/word/i",$field))
    {
        if(!preg_match($pass_patt, $value))
        {   
            $error_array[] = "Invalid $field";  
        }
     }
  } 

  $good_data[$field] = strip_tags(trim($value));

}

if(@sizeof($error_array) > 0) 
{

  $message = "<ul style='color: red; list-style: none' >";
  foreach($error_array as $value)
  {
   $message .= "<li>$value</li>";
  }
  $message .= "</ul>"; 

  echo $message;
  extract($good_data);
  include("login.inc");
  exit();   
}

$_SESSION 变量:

else
    {
    foreach($good_data as $field => $value)
        {
            $good_data[$field] = mysqli_real_escape_string($cxn,$value);
        }
        $sql = "SELECT * from UserInfo where user_id = '$good_data[user_id]' and
        password = '$good_data[password]'";
            $result = mysqli_query($cxn,$sql) or die("Couldn't find UserInfo: " . mysqli_error($cxn));

        if ( mysqli_num_rows($result) > 0) 
        {           
        $sql2 = "UPDATE TimeStamp SET user_id = '$good_data[user_id]', time = CURRENT_TIMESTAMP";  
        $result2 = mysqli_query($cxn,$sql2) or die("Couldn't update TimeStamp: " . mysqli_error($cxn)); 

        $_SESSION["variable"] = "condition";
        header("location: products.php");
        }
    } 
}
else
{
  $user_id = "";
  $password = "";   
  include("login.inc");
}
?>

products.php 页面:

    <?php
    session_start();
    include(connection.php);
    if(!isset($_SESSION['variable']) or $_SESSION['variable'] != "condition")
        {
            header("location: login.php");
            exit(); 
        }
?>

【问题讨论】:

  • 请提供您页面的完整代码,代码不完整。
  • @captain_a 有页面的完整代码
  • 如果我打印 $_SESSION,它会返回“array”,所以 $_SESSION 数组中没有任何内容,但不知道为什么。
  • 你必须使用'print_r($_SESSION);'看会议..
  • connection.php 中可能有问题.. 你能显示它的代码吗?

标签: php session


【解决方案1】:

查询中的问题, 查询返回结果了吗?

$sql = "SELECT * from UserInfo where user_id = '$good_data[user_id]' and
        password='$good_data[password]'";

改成:

$sql = "SELECT * from UserInfo where user_id = '{$good_data['user_id']}' and
        password='{$good_data['password']}'";

另外:

  • 阅读SQL Injection 并转义参数
  • 将您的密码保存在某种哈希值中,例如 md5 或更安全的格式

【讨论】:

  • 好吧,即使我使用的是 5.4,password_hash 函数似乎也不适用于我的 php 版本。所以我像你说的那样放了coide,我得到了同样的结果。
  • $sql = "SELECT * from UserInfo where user_id = '{$good_data['user_id']}' and password = '{$good_data['password']}'"; crypt($good_data['password']);
  • 打印 sql 和结果 ($row) 看看你得到了什么
  • 打印 $sql 和 $row 得到相同的结果,在我添加 else 语句 $SESSION 之前,您可以注册和登录没有问题。只有当我尝试让它存储会话时
  • 如果我理解代码错误是如果用户成功那么你需要存储会话 $_SESSION['variable'] = "condition" 不在 else 中
猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
相关资源
最近更新 更多