Hello,
I am trying to recall the $username in the private page (e.g. Welcome $username to your private page) and I think I'm having some difficulties. Actually I made working once before Christmas but now It drives me crazy. It's an assignment for the college.
this the URL:
http://www.dcs.bbk.ac.uk/~pparod01/index.php
if you insert ID: polo & Password: polo on the next page will appear this heading:
Welcome "Stranger" to your private page!
Thank you for your help.
They are basically three files:
LOGIN.PHP
<?php
$username = 'username';
$password = 'password';
$login=false;
if (isset($_POST["username"]) && isset($_POST["password"])) {
// open text.txt for reading
$file = fopen("text.txt","r");
while (!feof($file)) {
$data = explode ("|", fgets($file));
if(isset($data[0]) && isset($data[1])){
if (trim($data[0]) == trim($_POST["username"]) && trim($data[1]) == trim($_POST["password"])) {
$login = true;
$_SESSION["login"] = $login;
$_SESSION["username"] = $_POST["username"];
//$_SESSION['type'] = $data[3];
echo "Thank you for logging in, in 5 seconds you will be taken to the homepage.";
header("refresh: 5; private.php");
break;
}
}
}
if (!$login)
{
echo 'Login Failed. Perhaps you havent registered yet? <a href="register.php">Register Here</a>';
}
fclose($file);
}
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<table>
<tr align="right">
<td><label>Username:</label></td>
<td><input name="username" type="text" id="user"></td>
</tr>
<tr align="right">
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pword"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" id="login" value="login">
<a href="http://www.dcs.bbk.ac.uk/~pparod01/register.php">Register</a></td>
</tr>
</table>
</fieldset>
NAME.PHP
<?php
/**
* Name.
*/
if (isset($_SESSION['username']))
{
$username = $_SESSION['username'];
}
else
{
$name = 'Stranger';
}
if (isset($_SESSION['email']))
{
echo '<a href="mailto:'.htmlentities($_SESSION['email']).'">'. htmlentities($name) .'</a>';
}
else
{
echo htmlentities($name);
}
?>
PRIVATE.PHP
<?php
session_start();
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Private</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Welcome <?php require('name.php'); ?> to your private page!</h1>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin in felis mollis
massa molestie consectetuer. Aliquam eget odio. Ut quam diam, pharetra non, porttitor
sed, venenatis id, risus. Fusce imperdiet molestie lacus. Pellentesque habitant morbi
tristique senectus et netus et malesuada fames ac turpis egestas. Proin porttitor
fermentum augue. Ut egestas, nunc non porta congue, dolor nisi luctus pede, quis
porttitor enim magna et mi.
</p>
<p>
Quisque leo. Morbi id nisi tincidunt leo aliquam tincidunt.
Ut rutrum, velit ac porta scelerisque, est mauris egestas urna, vel volutpat risus pede
a eros. Aenean aliquam ante pulvinar eros. Cras lacinia, urna vitae laoreet ultricies,
dui erat vestibulum nulla, lacinia fringilla elit turpis id augue.</p>
<table width="225">
<tr>
<td width="88"><a href="http://www.dcs.bbk.ac.uk/~pparod01/index.php">Home Page</a></td>
<td width="32"> </td>
<td width="89"><a href="http://www.dcs.bbk.ac.uk/~pparod01/loggedout.php">Logout</a></td>
</tr>
</table>
</body>
</html>