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">&nbsp;</td>
  <td width="89"><a href="http://www.dcs.bbk.ac.uk/~pparod01/loggedout.php">Logout</a></td>
 </tr>
</table>
</body>
</html>

on EVERY page where you are using $_SESSION, you MUST call session_start() first. So, at the very top of your pages, BEGIN with:

<?php
session_start();
//rest of your code follows
...
?>

That could be a cause and I have added start_session() in all pages...but still doesn't work. Thank you for your help

You need it at the beginning of ALL three files. Also, in Name.php you have: if (isset($_SESSION['email'])) BUT in LOGIN.php you are NOT initializing $_SESSION['email'] anywhere. All you have is:

$_SESSION["login"] = $login;
$_SESSION["username"] = $_POST["username"];

Maybe you meant: if (isset($_SESSION['login']))

Please don't bother $_SESSION as I do not recall it.

The page that is missing a line of php is PRIVATE.PHP

<!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>

<?php
session_start();
require ('name.php');
?>


<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">&nbsp;</td>
<td width="89"><a href="http://www.dcs.bbk.ac.uk/~pparod01/loggedout.php">Logout</a></td>
</tr>
</table>
</body>
</html>

and then other page that may needs to be changed is NAME.PHP

<?php

//I have tried to add(isset($_SESSION))

if (isset($_POST)) //I tried with $_SESSION as well
{
$username = $_POST; //I tried with $_SESSION as well
}
else
{
$username = 'Stranger';
}

?>

again

<?php
session_start();
require ('name.php');
?>

needs to be at the BEGINNING of the FILE. You have it in the middle.

I am trying but it doesn't work.

In the middle is right as I copied from the college exercise:

<h1>Welcome<?php require ('name.php'); ?> to your private page!</h1>

I don't know where your have

<h1>Welcome<?php require ('name.php'); ?> to your private page!</h1>

but the problem is that on the file where you are including FROM, you already send output to the browser (since in your code <h1>Welcome... appears before the include).

Let's assume that the page you are working on is at http://yoursite.com/yourpage.php and that yourpage.php includes name.php.

Then you need to call session_start() at the very top of yourpage.php and NOT at name.php. All this confusion is because you did not provide enough details about your application. When working with sessions and includes, you need to be very clear as to which files are including other files.

I have done it, thank you very much for your help and patience.

This was the key to solve it:
In Private.php
<?php Require ('name.php'); ?>

And

welcome<?php echo $username ;?>to your private page

Glad to help.

Regards,
Hielo

PS: Don't forget to mark the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.