2

How do I get my Username and password from one php page that checks to see if the username and password is correct then to another which simply prints out what the user typed in on the page before.

I don't want to use 'include' because that executes my entire php file which will not work because it runs the login form twice and it then redirects me to my invalid login form page.

On a lot of website where you login in on one page then on the next page it says welcome, your name, this is pretty much what I am trying to do.

I have tried to include it, used ob_start() and then include then ob_end_clean(), after the user types they info I tried to put that in one php file, all of these didn't work. I feel it is a lot simpler than all of this but I just don't know how to do it.

Here is my php file that checks if the username and password are correct.

<?php
session_start();
$usrArray = array("Shawn","Terri","RJ","Mark");
$pwdArray = array("sjam96","terri","rj","mark");
$usr = $_SESSION['username'];
$pwd = $_SESSION['password'];
$valid = 0;

for($i=0; $i<4; $i++){
    if(($usr == $usrArray[$i]) && ($pwd == $pwdArray[$i])){
    $valid = 1;
    break;
    }
}

if ($valid > 0){
    header ("Location: futurePoolPageCheck.php");
}else {
    header ("Location: Invlaid.html");
}
?>

then this is where it prints it out on the next page:

<?php
session_start();
$usr = $_SESSION['username'];
echo $usr;
echo $pwd;
?>
4

1 回答 1

1

当用户成功登录时,您需要将详细信息设置为会话。在您运行登录查询以检查密码、用户名、电子邮件地址等是否匹配后,将用户 ID 放在变量中,例如 $id= $row['id'] 并包含以下行:

$_SESSION['user'] = $id;

然后,您可以使用会话作为参考从任何页面上的数据库中提取结果。

或者,如果它只是您要显示的名称,您可以将会话设置为用户的名称,例如

$_SESSION['username'] = $row['username'];

然后,您可以检查会话是否已设置并将其回显到您需要使用它的任何地方。

这些只是基本示例,但应该让您知道从那里做什么。

于 2016-08-22T02:28:06.003 回答