我是php的新手 我正在尝试在php中创建登录会话。我创建了一个表单,其操作转到process_login.php process_login.php中的codse是:
<?php
include_once 'sample/config.php';
include_once 'function.php';
sec_session_start();
if (isset($_POST['username'], $_POST['p'])) {
$username = $_POST['username'];
$password = $_POST['p'];
if (login($username, $password, $mysqli) == true) {
// Login success
header('Location: protected_page.php');
} else {
// Login failed
header('Location: admin_login.php?error=1');
}
} else {
echo 'Invalid Request';
}
?>
这是登录页面代码:
<?php
include('sample/config.php');
include_once 'function.php';
sec_session_start();
if (login_check($mysqli) == true) {
$logged = 'in';
} else {
$logged = 'out';
}
?>
<form action="process_login.php" method="post" name="login_form">
<label for="username">User name</label><br>
<input type="text" class="username" id="username" name="username" required>
<label for="password">Password</label><br>
<input type="password" class="password" id="password" name="password" required>
<button type="submit" class="submit" onClick="formhash(this.form, this.form.password);">Sign in</button>
</form>
登录后,此脚本必须将页面重定向到protected_page.php,或者在失败的情况下,必须转到admin_login.php。
但这暂时不起作用。在我点击提交后,它在url中显示了我的process.php。
答案 0 :(得分:3)
尝试使用session_start()而不是sec_session_start()。 如果您想要安全会话,则必须使用http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL
创建sec_session_start函数答案 1 :(得分:0)
你是什么意思process.php?它是提交表单的初始页面吗? 请确保在表格标签中操作=&#34; process_login.php&#34;包括在内。
答案 2 :(得分:0)
尝试session_start()
以及你的if检查条件错误的帖子值
session_start();
if (isset($_POST['username']) && isset( $_POST['p'])) {
$username = $_POST['username'];
$password = $_POST['p'];
if (login($username, $password, $mysqli) == true) {
// Login success
header('Location: protected_page.php');
} else {
// Login failed
header('Location: admin_login.php?error=1');
}
} else {
echo 'Invalid Request';
}
?>
你的代码错误
sec_session_start();
if (isset($_POST['username'], $_POST['p'])) {
login()
函数的作用是什么$mysqli
请检查此代码是否正常
if (login($username, $password, $mysqli) == true) {