PHP代码无法按预期工作

时间:2014-11-07 07:27:31

标签: php session login

当我输入用户名和密码时,一切正常,但是当我点击按钮' page3'代码答案"你必须先登录",也许你知道这是什么问题?

使page2.php

    <?php
session_start();
?>
<html>
<head>
</head>
<body>
<a href="page3.php">page3</a>
<?php
$dbname = "loggggg";
$dbpass = "vabvjhabj";
if(isset($_POST['submitlogin'])){
$username = $_POST['usernameinput'];
$password = $_POST['passwordinput'];
if($username == $dbname){
if($password == $dbpass){
$_session['currentuser'] = $username;
$_session['currentaccesslevel'] = 5;
echo "welcome back, " . $username;
}
else {
showform('wrong password');
    }
}
else {
showform("username not found");
}
}
else {
showform("please enter your username and password");
}
function showform($message){
include ('form.php');
echo $message;
}
?>

form.php的

    <html>
<head>
</head>
<body>

<form method="post" action="page2.php" />
<input type="text" name="usernameinput" />
<input type="text" name="passwordinput" />
<input type='submit' value='submit' name='submitlogin' />
</form>

</body>
</html>

page3.php

 <?php
session_start();

if(isset($_session['currentuser'])) {
    echo "welcome " . $_session['currentuser'] . "this is your profile";
} else
echo "you must login first";
?>

1 个答案:

答案 0 :(得分:1)

改变 -

on page2

$_SESSION['currentuser'] = $username;
$_SESSION['currentaccesslevel'] = 5;

on page3

if(isset($_SESSION['currentuser'])) {
    echo "welcome " . $_SESSION['currentuser'] . "this is your profile";
}