Arevik Akopian
Arevik Akopian

Reputation: 17

PHP code does not work as expected

When I enter the username and password everything works fine, but then, when I click on the button 'page3' code answers "you must login first", maybe you know what is the problem?

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";
?>

Upvotes: 1

Views: 70

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

change -

on page2

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

and

on page3

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

Upvotes: 1

Related Questions