gpt4 book ai didi

php - 全局变量与 session_start();

转载 作者:行者123 更新时间:2023-11-29 18:04:46 27 4
gpt4 key购买 nike

我使用 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>

最佳答案

我认为您应该将 $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>

关于php - 全局变量与 session_start();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47977562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com