Php 关于欢迎登录用户的问题
我正在尝试使用PHP登录系统在受限区域添加一个欢迎用户的功能。我使用这段代码将usernale从第一个用户登录页面转移到受限页面,但它不起作用Php 关于欢迎登录用户的问题,php,Php,我正在尝试使用PHP登录系统在受限区域添加一个欢迎用户的功能。我使用这段代码将usernale从第一个用户登录页面转移到受限页面,但它不起作用 if ( (isset($_POST['username'])) || (isset($_POST['password'])) { $user = $_SESSION['username']; } 这是我正在使用的文件,你能看一下,让我知道我做错了什么吗 <form id="login-for
if ( (isset($_POST['username'])) || (isset($_POST['password'])) {
$user = $_SESSION['username'];
}
这是我正在使用的文件,你能看一下,让我知道我做错了什么吗
<form id="login-form" method="post" action="includes/login.inc.php">
<fieldset>
<legend>Login to Web Site</legend>
<label for="username">
<input type="text" name="username" id="username" />Username:
</label>
<label for="password">
<input type="password" name="password" id="password" />Password:
</label>
<label for="submit">
<input type="submit" name="submit" id="submit" value="Login" />
</label>
</fieldset>
登录网站
用户名:
密码:
我有一个php登录文件,如下所示:
<?php
require_once('config.inc.php');
require_once('functions.inc.php');
// Start session
session_start();
// Check if user is already logged in
if ($_SESSION['logged_in'] == true) {
// If user is already logged in, redirect to main page
redirect('../index.php');
} else {
// Make sure that user submitted a username/password and username only consists of alphanumeric chars
if ( (!isset($_POST['username'])) || (!isset($_POST['password'])) OR
(!ctype_alnum($_POST['username'])) ) {
redirect('../login.php');
}
// Connect to database
$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if (mysqli_connect_errno()) {
printf("Unable to connect to database: %s", mysqli_connect_error());
exit();
}
// Escape any unsafe characters before querying database
$username = $mysqli->real_escape_string($_POST['username']);
$password = $mysqli->real_escape_string($_POST['password']);
// Construct SQL statement for query & execute
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . md5($password) . "'";
$result = $mysqli->query($sql);
// If one row is returned, username and password are valid
if (is_object($result) && $result->num_rows == 1) {
// Set session variable for login status to true
$_SESSION['logged_in'] = true;
redirect('../index.php');
} else {
// If number of rows returned is not one, redirect back to login screen
redirect('../login.php');
}
}
?>
在我的受限页面上,我有:
<?php
// Start session
session_start();
if ( (isset($_POST['username'])) || (isset($_POST['password'])) {
$user = $_SESSION['username'];
}
require_once('includes/functions.inc.php');
if (check_login_status() == false) {
redirect('login.php');
}
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<a class="welcome">
Welcome: <?php echo $user; ?>
</a>
</div>
使用
$\会话['username']=$\发布['username']
然后回显$\u会话['username']
编辑
<?php
// Start session
session_start();
if (isset($_POST['username']) || isset($_POST['password'])) {
require_once('includes/functions.inc.php');
if (check_login_status() == false) {
redirect('login.php');
}
$_SESSION['username'] = $_POST['username'];
}
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<a class="welcome">
Welcome: <?php echo $_SESSION['username']; ?>
</a>
</div>
欢迎:
当用户登录时,您没有设置$\u SESSION['username']的值,而没有阅读您的所有代码,当用户登录时,它调用函数redirect()
,我假设该函数重定向浏览器。这就是为什么你没有得到$\u POST
数据的原因。嗨,凯文,谢谢你的代码和评论,但我仍然在未定义的索引用户名上遇到错误!欢迎:(!)注意:未定义索引:C:\wamp\www\LoginApp_3\index.php中的用户名在第102行调用堆栈#时间内存函数位置10.0006 692704{main}()..\index.php:0确保您的$POST变量有信息,执行打印($POST)以检查发送的POST数据中的信息!我不确定我是否做得正确,但我使用了这段代码并得到了相同的错误