带有session_start()的全局变量;

时间:2017-12-26 10:58:10

标签: php mysql mysqli

我用php和mysql创建了一个登录表单,

这是登录后应该出现的页面:

<?php
session_start();
   require_once 'helper.php';

if( ! isset ($_SESSION['user_id'] )){

    header('location: index.php');
}
  ?>

 <?php
include 'header.php';
?>

如果用户未登录,我添加了此代码以禁止访问该页面。 我的问题是,在我添加此代码后,我无法使用之前登录的用户登录。 我想这是因为它无法识别全局

  var $_SESSION['user_id']

但我不明白为什么,session_start();是在页面的乞讨。

这是登录页面:

<?php
session_start();
require_once 'helper.php';

$error = '';

if (isset($_POST['submit'])) {
    $email = !empty($_POST['email']) ?
            trim($_POST['email']) : '';
    $password = !empty($_POST['password']) ?
            trim($_POST['password']) : '';

    if (!$email) {
        $error = '*הכנס אימייל';
    } elseif (!$password) {

        $error = '*הכנס סיסמה';
    } else {

        $sql = "SELECT * FROM users WHERE email = '$email' AND 
          password = '$password' ";
        $link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
        $result = mysqli_query($link, $sql);

        if ($result && mysqli_num_rows($result) > 0) {
            $user = mysqli_fetch_assoc($result);
            $_SESSION['user_id'] = $user['íd'];

            $_SESSION['user_name'] = $user['name'];

            header('location: posts.php');
        } else {
            $error = 'אימייל או סיסמה לא נכונים';
        }
    }
}
?>
<?php
include("header.php");
?>
<div id="space-signup"></div>
<div class="container">
    <section id="signup">
        <form action="" method="post" style="width:300px; margin:0 auto; ">

            <input type="email" name="email" placeholder="אימייל"  value="<?= old('email'); ?>" class="form-control">

            <input type="password" name="password" id="password"  
                   placeholder="סיסמה" class="form-control">

            <button type="submit" class="btn" name="submit" value="sign in"  
                    style="width:300px" >התחבר</button>
            <span class="sign-in-error"> <?= $error ?></span>
        </form>
    </section>
</div>

2 个答案:

答案 0 :(得分:0)

我认为您应该将$user['íd']替换为$user['id']或您的列名称。 请参阅以下修改后的代码。

<?php
session_start();
require_once 'helper.php';

$error = '';

if (isset($_POST['submit'])) {
    $email = !empty($_POST['email']) ?
            trim($_POST['email']) : '';
    $password = !empty($_POST['password']) ?
            trim($_POST['password']) : '';

    if (!$email) {
        $error = '*הכנס אימייל';
    } elseif (!$password) {

        $error = '*הכנס סיסמה';
    } else {

        $sql = "SELECT * FROM users WHERE email = '$email' AND 
          password = '$password' ";
        $link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
        $result = mysqli_query($link, $sql);

        if ($result && mysqli_num_rows($result) > 0) {
            $user = mysqli_fetch_assoc($result);
            $_SESSION['user_id'] = $user['id'];

            $_SESSION['user_name'] = $user['name'];

            header('location: posts.php');
        } else {
            $error = 'אימייל או סיסמה לא נכונים';
        }
    }
}
?>
<?php
include("header.php");
?>
<div id="space-signup"></div>
<div class="container">
    <section id="signup">
        <form action="" method="post" style="width:300px; margin:0 auto; ">

            <input type="email" name="email" placeholder="אימייל"  value="<?= old('email'); ?>" class="form-control">

            <input type="password" name="password" id="password"  
                   placeholder="סיסמה" class="form-control">

            <button type="submit" class="btn" name="submit" value="sign in"  
                    style="width:300px" >התחבר</button>
            <span class="sign-in-error"> <?= $error ?></span>
        </form>
    </section>
</div>

答案 1 :(得分:0)

尝试在开始会话之前致电ob_start(),因为 如果默认情况下你的output_buffering为Off并且你不幸将单个字节的数据发送回客户端,那么你的HTTP头已经被发送了。这有效地阻止了session_start()将cookie头传递回客户端。通过调用ob_start(),您可以启用缓冲,从而延迟发送http标头。