1

设计用户成员资格的另一种尝试。必须登录成功,在数据库中找到数据。但是在我的索引文件中,登录后,它应该检查我是否已登录并显示指向我的帐户的链接,而不是注册和登录。这是代码:

<?php
session_start(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
// Put stored session variables into local php variable
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '<a href="member_profile.php?id=' . $userid . '">' . $username . '</a>           &bull; 
<a href="member_account.php">Account</a> &bull; 
<a href="logout.php">Log Out</a>';
} else {
$toplinks = '<a href="join_form.php">Register</a> &bull; <a     href="login.php">Login</a>';
}
?>

这是登录表单代码,我认为问题在于它没有存储我的会话ID:

<?php
if ($_POST['email']) {
//Connect to the database through our include 
include_once "connect_to_mysql.php";
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = preg_replace("[^A-Za-z0-9]", "", $_POST['password']); 
// filter everything but numbers and letters
$password = md5($password);
// Make query and then register all database data that -
// cannot be changed by member into SESSION variables.
// Data that you want member to be able to change -
// should never be set into a SESSION variable.
$sql = mysql_query("SELECT * FROM users WHERE email='$email' AND password=
'$password'AND emailactivated='1'"); 
$login_check = mysql_num_rows($sql);
if($login_check > 0){ 
while($row = mysql_fetch_assoc($sql)){ 
    // Get member ID into a session variable
    $userid = $row["id"];   
    $_SESSION['id'] = $userid;
    // Get member username into a session variable
    $username = $row["username"];
    $_SESSION['username'] = $username;
    // Update last_log_date field for this member now
    mysql_query("UPDATE users SET lastlogin=now() WHERE id='$userid'"); 
    // Print success message here if all went well then exit the script
    header("location: member_profile.php?id=$userid"); 
    exit();
} // close while
} else {
// Print login failure message to the user and link them back to your login page
print '<br /><br /><font color="#FF0000">No match in our records, try again 
</font>  <br/>
<br /><a href="login.php">Click here</a> to go back to the login page.';
exit();
}
}// close if post
?>

我再次遵循某人的教程并尝试将其实施到我的网站,如果它有效,这将是完美的。请告知为什么在登录后没有设置 $toplinks。

4

2 回答 2

3

我认为问题在于,您必须session_start()在要使用会话的每个文件中包含 。否则它像普通数组一样在文件中工作,但不是全局的。在您的表格中,我看不到您开始会话。

编辑:仅当您有 2 个文件时才需要此文件。当您只有一个文件并包含另一个页面时,当您在顶部包含一次时,它的工作原理。

于 2012-12-15T01:05:48.560 回答
1

如果你想注销,那么你应该创建一个注销文件,并包括

session_destroy();

可能还添加一个 href 以通过执行以下操作来获取重定向链接:

header('location:index.php'); // will return you to index as soon as you logout.

于 2012-12-15T01:36:12.067 回答