Jump to content

[SOLVED] PHP, Session States, MySQL, Apache and Server Side Includes ( virtual() )


Galaxy_Stranger

Recommended Posts

Several months ago I created a site for a class that had user logins and I used session states to keep track of who was logged in.  All of this was checked against a MySQL database and everything worked exactly the way I wanted.

 

I am now using the same technique on a new site, but this time I'm using PHP's virtual() function to suck-in the menu.  I got everything set up and the log-in script actually works, but the menu script on the menu html page isn't changing based on the session variables.

 

In other words, the menu is included using virtual() - and shows up, but the script on the menu page itself is acting like the session variables haven't been changed, like there's some sort of disconnect.

 

My question is - do session variables have to be passed to only one page at a time?  Or are they consistent throughout the session?

 

TIA

Ok - as it stands, basically every page includes the menu using virtual().  Here is the index page code:

<!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> Welcome! </title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<link rel="STYLESHEET" href="index.css" type="text/css" />
	<script type="text/javascript" src="javascript.js"></script>
</head>

<body>
<div id="body_content">
<table class="body_table" border="0" width="100%">
	<tr>
		<td>
		</td>
		<td align="center">
		<?php virtual("header.php") ;?>
		</td>
		<td>
		</td>
	</tr>
	<tr>
		<td align="left">
		<?php virtual("menu.php") ;?>
		</td>
		<td>
		<?php virtual("news.php") ;?>
		</td>
		<td align="left">
		<?php virtual("right.php") ;?>
		</td>
	</tr>
	<tr>
		<td>
		</td>
		<td align="center">
		<?php virtual("footer.php") ;?>
		</td>
		<td>
		</td>
	</tr>
</table>
</div>
</body>
</html>

 

Here is the menu code:

<?php
[color=blue]//Check to see if a session already exists.  If not, create variables.
//If yes, do nothing.[/color]
session_start() ;
if(!isset($_SESSION['loggedIn']))
{
session_register('loggedIn') ;
session_register('currentUser') ;
$_SESSION['loggedIn'] = NO ;
}
else
{
}
?>

<ul class="square">
	<li><a href="index.php">Home</a></li>
	<li><a href="about.php">About</a></li>
	<li><a href="contact.php">Contact</a></li>
</ul>
<br />

<?php
if($_SESSION[loggedIn] == NO)
{
echo "<h2><a href=\"login.php\">Login</a></h2>" ;
echo "<h2><a href=\"register.php\">Register</a></h2>" ;
[color=blue]<!--  Code below is to test the value of the session variable -->[/color]
	echo "Is logged-in set to YES?" ;
	echo "$_SESSION[loggedIn]" ;
}
elseif($_SESSION[loggedIn] == YES)
{
echo "<h2>Logged-in as: $_SESSION[currentUser].</h2>" ;
echo "<h2><a href=\"logout.php\">Not you? Logout</a></h2>" ;

[color=blue]<!--  Code below is to test the value of the session variable -->[/color]
	echo "Is logged-in set to YES?" ;
	echo "$_SESSION[loggedIn]" ;
}
?>

<br />
<a href="http://validator.w3.org/check?uri=referer"><img src="/images/Validation/xhtml1.0.png" alt="Valid XHTML 1.0 Strict" height="15" width="80" /></a>
<br />
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img style="border:0;width:80px;height:15px" src="/images/Validation/css.png" alt="Valid CSS!" /></a>
<br />

 

The user can click on the Log-in link and is taken to the log-in page, which is just a simple form for inputting the username and password.  It is submitted and sent to 'verifyLogin.php' which has the same includes as the index page.  The only difference is that the body content included is the verify script:

 

			<h1>Verify Login</h1>


<?php

$userName=$_POST[userName] ;     [color=blue]//  Retrieve variable userName posted from log-in form.[/color]
$password=$_POST[password] ;      [color=blue]//  Retrieve variable password posted from log-in form.[/color]

[color=blue]//  Create connection to the database:[/color]
$connection = mysql_connect('localhost', '<mysterious_admin_name>', '<mysterious_password>') or die(mysql_error()) ;
$database = mysql_select_db('<myDatabase>', $connection) or die(mysql_error()) ;
$sql="select userID from userlogins where userID='$userName' and password='$password'" ;
$result=mysql_query($sql)  or die(mysql_error()) ;

if(mysql_num_rows($result) == 1)
{
$_SESSION['loggedIn'] = YES ;                    [color=blue]//  Change value of session variable 'loggedIn' to YES.[/color]
$_SESSION['currentUser'] = $userName ;       [color=blue]// Change value of session variable 'currentUser' to $userName value.[/color]

        [color=blue]//  Kill the database connection:[/color]
$closeDatabase = mysql_close($connection) ;
	if(!closeDatabase)
	{
		die(mysql_error()) ;
	}

//header("Location: index.php") ;    [color=blue]//  I commented this out to use the code below, and it wasn't working anyway.  It would actually kick back to the verifyLogin page.[/color]
//exit() ;

	echo "Login successful, $_SESSION[currentUser].<br />" ;
	echo "Is logged-in set to YES?" ;
	echo "$_SESSION[loggedIn]" ;
                [color=blue]//  The three lines of code above run and reflect the changed values of the session variables.[/color]


}
else          [color=blue]//  This code runs and displays the correct output if I enter in an incorrect username and/or password.[/color]
{
                [color=blue]//  Kill the database connection[/color]
	$closeDatabase = mysql_close($connection) ;
		if(!closeDatabase)
		{
			die(mysql_error()) ;
		}
	echo "Username and/or password did not match.  Please try again.<br />" ;
	echo "<form method=\"POST\" action=\"verifyLogin.php\">" ;
	echo "<h2>User Name: <input type=\"text\" name=\"userName\" size=\"50\" maxlength=\"50\"/></h2>" ;
	echo "<h2>Password: <input type=\"password\" name=\"password\" size=\"50\" maxlength=\"50\"/></h2>" ;

	echo "<h2><input type=\"submit\" name=\"submit\" value=\"LOGIN\"/></h2>" ;
	echo "</form><br />" ;
	echo "<hr />" ;
}
?>

Ok - for some reason everything has decided to work.  I removed the session_register() lines, refreshed the pages - still didn't work.  I went and played around with using .php files in the Apache server side includes - which didn't work at all, so I set them back to .html.  I restarted the server and everything decided to work.

 

I have no real idea what went wrong, except that it had something to do with restarting Apache, apparently.

 

Thanks for all the help.

Just in case anyone's interested - I found out what my problem was:

 

"session_start() ; " needs to be the FIRST element in the body of each web page when using virtual() to include content. Otherwise, session variables won't be accessible by all content.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.