php - $_SESSION 不携带跨 php 页面

标签 php mysql session


我的 php 网站中的 $_SESSION 函数有问题。我设置了该功能,然后尝试在另一个页面中访问它,但它只是显示为空白。

这是我设置 $_SESSION

的文件 login.php
<?php
session_start();
?>
<html>
    <?php
    $email = $_POST['email']; 
    $password = $_POST['password'];
    $con = mysqli_connect("host","user","pass","db");
if (!$con)
  {
  die('Could not connect: ' . mysqli_error());
  }

  if ($email && $password){
    $result = mysqli_query($con, "SELECT * FROM users WHERE email='$email'");
    $numrows = mysqli_num_rows($result);

    if ($numrows != 0){ 
        //username exists
        $row = mysqli_fetch_row($result);

        if ($password != $row[2]){
            print 'Incorrect password<br />';
            $error = TRUE;
        }
        else {
            //password is correct
            echo "You're in! <a href='member.php'>Click</a> here to enter member page";
            $_SESSION['email'] = $email;
        }
    }
    else {
        //email not found
        print 'Sorry, that email cannot be found.<br />';
        $error = TRUE;
    }
  }
  else {
    die('Please enter an email and password<br />');
    $error = TRUE;
  }

  if ($error){
    print '<a href=index.php>Go Back</a>';
  }
    ?>
</html>

这是 member.php 文件。

<?php
session_start();
if (isset($_SESSION['email'])){
    $username = $_SESSION['email'];
    echo "Your email is ";
    echo $username;
}
?>

当我按链接通过链接转到member.php 页面时,该页面完全空白,大概是在暗示$_SESSION['email'] 里面什么都没有。

请让我知道我哪里出错了,以及如何纠正这个问题。
任何帮助将不胜感激。

如果有人想查看该站点的实际运行情况,请转到 here .

最佳答案

添加session_start();在 login.php 中的 HTML 之前设置它

关于php - $_SESSION 不携带跨 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462866/

相关文章:

arrays - 如何将数组存储到 session 中并在 Laravel 中调用它?

java - xPage NotesContext getDatabase ACL 问题

php - 在 WooCommerce 中的正常价格之前显示销售价格

javascript - jsPDF 没有创建正确的 PDF

php - 为什么我不能在 js 中这样做

mysql - 对 MySQL 中跨日期和位置的实体共现进行计数和排序

php - 如何用php获取mysql数据库的行数

php - 从数组中获取前 5 个最大值

mysql - 选择具有相同 id 的行并重写按 id 分组的 id

session - 使用 UserCake 注销太快