Session Issues

Years ago, I tried learning PHP. I learned just enough to get myself into trouble. I’m trying to set up a section on a website now where a user can log in and see a page that has links to PDF files. The links will change for each user.

It seems like I have everything working for the most part, even though I’m sure my code is horrible. I’ll post all my code and then ask my question below.

broker-login.html

    <form method="POST" action="broker-login.php"> 
    <table width="200" border="0" align="center" cellpadding="0" cellspacing="5">
      <tr>
        <td width="200">Username:</td>
        <td width="200"> <input type="text" name="username" size="20" /></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="password" size="20" /></td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" name="login" /></td>
        </tr>
    </table>
    </form>

broker-login.php
[php]<?php
//set the level of error reporting
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

//convert the field values to simple variables

//add slashes to the username and md5() the password
$user = $_POST[‘username’];
$pass = $_POST[‘password’];

//set the database connection variables

$dbHost = “xxx”;
$dbUser = “xxx”;
$dbPass = “xxx”;
$dbDatabase = “xxx”;

//connet to the database

$db = mysql_connect("$dbHost", “$dbUser”, “$dbPass”) or die (“Error connecting to database.”);

mysql_select_db("$dbDatabase", $db) or die (“Couldn’t select the database.”);

$result=mysql_query(“select * from vendors where username = ‘$user’ AND password = ‘$pass’”, $db);

//check that at least one row was returned

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

//start the session and register a variable

session_start();
session_register(‘username’);

//we will redirect the user to another page where we will make sure they’re logged in
header( “Location: http://savannahclassics.com/broker-user.php” );

}

}
else {

//if nothing is returned by the query, unsuccessful login code goes here…

?>

  <p>Login failed. Try again.</p>
<form method="POST" action="broker-login.php"> 
<table width="200" border="0" align="center" cellpadding="0" cellspacing="5">
  <tr>
    <td width="200">Username:</td>
    <td width="200"> <input type="text" name="username" size="20" /></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input type="password" name="password" size="20" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" value="Submit" name="login" /></td>
    </tr>
</table>
</form>
<?php } ?>[/php]

broker-user.php
[php]<?php
//set the level of error reporting
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

//start the session
session_start();

//check to make sure the session variable is registered
if(session_is_registered(‘username’)){

//the session variable is registered, the user is allowed to see anything that follows
$username = $_SESSION[‘username’];
?>

<p style="text-transform:capitalize;">Welcome, <?php echo $username; ?></p>
  <ul>
	<li><a href="<?php echo $username; ?>/pdf/file1.pdf">File 1</a></li>
	<li><a href="<?php echo $username; ?>/pdf/file2.pdf">File 2</a></li>
	<li><a href="<?php echo $username; ?>/pdf/file3.pdf">File 3</a></li>
	<li><a href="<?php echo $username; ?>/pdf/file4.pdf">File 4</a></li>
  </ul>
<?php ; } else{ //the session variable isn't registered, send them back to the login page header( "Location: http://savannahclassics.com/broker-login.html" ); } ?>[/php]

On the final page (broker-user.php) echo $username isn’t outputting anything. Any suggestions?

It looks like you are registering a non-existing variable in broker-login.php - You have not declared $username, only $user.

Note, as of version 4.1.0: “Use of $_SESSION is preferred.” To implement this and store the username properly, you would simple replace this…[php]session_register(‘username’);[/php]

With this:[php]$_SESSION[‘username’] = $user;[/php]

I believe this will fix your issue, let me know if not and I’ll look at it further.

jay

That solved it. Thank you!

Glad it worked for you. I double checked and it looks like as of 5.3.0 session_register was depreciated and it was removed in 5.4.0 - this is the current best way to handle this.

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service