我的主页上有一个登录脚本,用于我的主页上的登录表单。当用户将表单提交给登录时,他/她提交他/她的用户名和密码。
脚本访问的数据库具有从用户注册中存储的用户名,密码和电子邮件地址。
一旦用户成功登录,他/她就会被重定向到一个页面,该页面加载他们之前在页面上的“评论”,这些评论存储在同一数据库中的不同表中。
我需要将电子邮件从一个表发送到重定向页面上的查询。
以下是我处理Login的PHP代码代码:
<?php
//If the user has submitted the form
if(isset($_REQUEST['username'])){
//protect the posted value then store them to variables
$username = protect($_POST['username']);
$password = protect($_POST['password']);
//Check if the username or password boxes were not filled in
if(!$username || !$password){
//if not display an error message
echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
}else{
//if they were continue checking
//select all rows from the table where the username matches the one entered by the user
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
$num = mysql_num_rows($res);
//check if there was no match
if($num == 0){
//if none, display an error message
echo "<center>The <b>Username</b> you supplied does not exist!</center>";
}else{
//if there was a match continue checking
//select all rows where the username and password match the ones submitted by the user
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was no match
if($num == 0){
//if none display error message
echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
}else{
//if there was continue checking
//split all fields from the correct row into an associative array
$row = mysql_fetch_assoc($res);
//check to see if the user has not activated their account yet
if($row['active'] != 1){
//if not display error message
echo "<center>You have not yet <b>Activated</b> your account!</center>";
}else{
//if they have log them in
//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['uid'] = $row['id'];
//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
//redirect them to the usersonline page
echo 'REDIRECT';
}
}
}
}
exit;
}
?>
以下是重定向到页面的PHP代码:
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM comments
WHERE email='$_POST[email]' ORDER BY dt");
while($row = mysqli_fetch_array($result))
{
echo $row['dt'] ." " . $row['email'] . " " . $row['body'];
echo "<br>";
echo "<br>";
}
?>
我需要在第一个代码中添加一些内容,以便从用于验证登录信息的表中选取电子邮件地址,并将其发送到第二个代码以接收“评论”。我试过谷歌搜索答案,没有提出任何问题。请帮忙!
答案 0 :(得分:0)
由于您在代码中使用了$_SESSION
数组(可能是从某处复制过的),因此您可以类似地将电子邮件地址存储在同一个数组中。
$_SESSION['email'] = $row['email'];
在后一页中,您需要将$_POST['email']
替换为$_SESSION['email']
。