【发布时间】:2013-03-12 15:55:08
【问题描述】:
抱歉,如果这是一个冗长的帖子,但我需要问这个问题。好吧,我已经在几台服务器上尝试过这段代码,但它不能正常工作!
我一直在尝试将我的 PHP 脚本从 mysql 更改为 mysqli,到目前为止,失败多于成功。
注册表工作正常,它将记录添加到 mysql 数据库,它也会向用户发送一封电子邮件,没有任何错误。我在所有页面上都有这个,以确保我没有收到任何错误:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
登录表单也可以正常工作,它会将用户登录到他们的帐户。
但这就是问题所在,还有几个我一直面临的问题。
1- 页面顶部有一个简单的顶部链接,如果用户未登录,它将显示登录和注册,一旦用户登录,它将显示他们的用户名和注销链接。这似乎有它的想法拥有,因为它可以在一台服务器上运行,而不能在另一台服务器上运行。通过工作,我的意思是即使用户已登录,它也会在一台服务器上保持登录和注册链接。
2- 有一个 logout.php 文件应该注销用户并结束会话。但这又有了自己的想法。在较旧的 PHP 服务器上,它可以正常工作,但如果我再次刷新用户帐户 URL 上的页面,它会自动将用户重新登录。我在哪个浏览器中尝试以及清除缓存的次数都没有关系和饼干。它仍然会在页面刷新时将用户重新登录到帐户中。
另外,logout.php 文件在 php 版本 5.3.21 的服务器上不起作用,它不会让用户退出他们的帐户!!
这是 member.php 代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '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.php?id=' . $userid . '">' . $username . '</a> •
<a href="member.php">Account</a> •
<a href="logout.php">Log Out</a>';
} else {
$toplinks = '<a href="join_form.php">Register</a> • <a href="login.php">Login</a>';
}
?>
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = preg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
echo "Missing Data to Run";
exit();
}
//Connect to the database through our include
include_once "config/connect.php";
// Query member data from the database and ready it for display
$sql = "SELECT * FROM members WHERE id='$id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$count = mysqli_num_rows($query);
if ($count > 1) {
echo "There is no user with that id here.";
exit();
}
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row["username"];
$_SESSION['username'] = $username;
$userid = $row["id"];
$_SESSION['id'] = $userid;
// Convert the sign up date to be more readable by humans
$signupdate = strftime("%b %d, %Y", strtotime($row['signupdate']));
}
?>
这是 logout.php 文件:
<?php
session_start();
session_destroy();
if( isset($_SESSION['id'])){
header("location: index.php");
} else {
exit('<h2>Could not log you out, sorry the system encountered an error.</h2>');
}
?>
<html>
<body>
<?php echo "$msg"; ?><br>
<p><a href="index.php">Click here</a> to return to our home page </p>
</body>
</html>
任何帮助将不胜感激。
【问题讨论】:
-
PHP 手册有一些很好的例子:php.net/manual/en/function.session-destroy.php
-
PHP 是给我带来所有这些麻烦的那个。我从来没有理解过 PHP 手册中提到的单个代码或单词。这就是为什么我乞求有关谷歌和论坛以及 stackoverflow 的信息。