When the user inputs the correct login details into the form in script one how can I get my script 2 to correctly load and output the text in the code? The issue I’m having at the moment is that after the user logins in on script one, script two doesn’t display.
I used this website to help me and when I run the code on script one I got these errors.
E_NOTICE : type 8 – Undefined index: username – at line 2
E_NOTICE : type 8 – Undefined index: password – at line 3
In script two I got the error below.
Line : 44 – syntax error, unexpected ‘<’
SCRIPT 1
<?php
$user = $_POST["username"];
$pass = $_POST["password"];
$validated = false;
session_start();
if($user!=""&&$pass!="")
{
if($user=="test1"&&$pass=="test1")
$validated = true;
if($validated)
{
$_SESSION['login'] = "OK";
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
header('Location: protected.php');
}
else
{
$_SESSION['login'] = "";
echo "Invalid username or password.";
}
}
else $_SESSION['login'] = "";
?>
<html>
<body>
<h1>Login Page</h1>
<p>Please enter your username and password:</p>
<form action="login.php" method="post">
<table>
<tr>
<td align="right">Username: </td>
<td><input size=\"20\" type="text" size="20" maxlength="15" name="username"></td>
</tr>
<tr>
<td align="right">Password: </td>
<td><input size=\"20\" type="password" size="20" maxlength="15" name="password"></td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="left"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
SCRIPT 2
<html>
<body>
<h1>Login Page</h1>
<p>Please enter your username and password:</p>
<form action="login.php" method="post">
<table>
<tr>
<td align="right">Username: </td>
<td><input size=\"20\" type="text" size="20" maxlength="15" name="username"></td>
</tr>
<tr>
<td align="right">Password: </td>
<td><input size=\"20\" type="password" size="20" maxlength="15" name="password"></td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="left"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
session_start();
if($_SESSION['login'] != "OK")
{
header('Location: login.php');
exit();
}
?>
<html>
<head
<title>Protected Web Page</title>
</head>
<body>
<h1>Protected Web Page</h1>
<?php
echo "<p>You have successfully logged in!</p>";
echo <img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">;
echo "<p>Your username is: ";
echo $_SESSION['username'];
echo "<br/>";
echo "Your password is: ";
echo $_SESSION['password'];
echo "</p>";
?>
</body>
</html>