Page 1 of 1

Session Problem!!

Posted: Wed May 08, 2002 5:10 pm
by pHlange
For some reason when I log in, it goes to account.php, but If I go back to the index.php, It doesnt hold the session! Ive been trying everything on this, If anyone can see something wrong with this please let me know. I just cant see anything wrong with the session stuff.

p.s - I check the sessiondata and the set variables are always there!! And Im running PHP 4.10 on WIN32

INDEX.PHP

Code: Select all

<?
	session_start();
?>
<html>
	<head>
		<title></title>
		<style type="text/css">
		<!--
			A:link    &#123; COLOR: 003399; TEXT-DECORATION: none; font-weight: bold  &#125;
			A:visited &#123; COLOR: 003399; TEXT-DECORATION: none; font-weight: bold &#125;
			A:active  &#123; COLOR: 003399; TEXT-DECORATION: none; font-weight: bold &#125;
			A:hover   &#123; COLOR: FFFFFF; TEXT-DECORATION: none; font-weight: bold &#125;
		-->
	</style>
	</head>
<body bgcolor="#FFCC00" topmargin="30" leftmargin="0" marginwidth="0" marginheight="0">
	<table width="760" cellspacing="5" cellpadding="10" border="1" align="center" bordercolor="#EEEEEE">
		<tr>
			<td colspan="2" align="center" width="100%"><font color="#003399" face="verdana,arial,helvetica" size="2"><b></font></b></font></td>
		</tr>
		<tr>
			<td width="150" valign="top">
				<font color="#000000" face="verdana,arial,helvetica" size="2">
					&nbsp;<!--<p align="left"><a href="index.php?action=home">Home</a></p>-->
				</font>
			</td>
			<td width="610" valign="top">
			<? if(!isset($_SESSION&#1111;'username']) || !isset($_SESSION&#1111;'password']))&#123; ?>
				<form action="account.php" method="post">
					<table cellspacing="5" cellpadding="5" align="center" border="0">
						<tr>
							<td colspan="2"><font face="verdana,arial,helvetica" size="2">Members only! Please login to access this document.</font></td>
						</tr>
						<tr>
							<td><font face="verdana,arial,helvetica" size="2">Username</font></td>
							<td><input type="text" name="username"></td>
						</tr>
						<tr>
							<td><font face="verdana,arial,helvetica" size="2">Password</font></td>
							<td><input type="password" name="password"></td>
						</tr>
						<tr>
							<td colspan="2" align="right"><input type="submit" name="check" value="Log In"></td>
						</tr>
					</table>
				</form>
				<? &#125;else&#123; ?>

					<font face="verdana,arial,helvetica" size="2">YOU ARE STILL LOGGED IN!</font>

				<? &#125; ?>
			</td>
		</tr>
	</table>
</body>
</html>
ACCOUNT.PHP

Code: Select all

<?
	session_start();

	if(empty($_POST&#1111;'username']) || empty($_POST&#1111;'password'])){

		echo "<font face="verdana,arial,helvetica" size="2">Error! - You did not supply the required details..You will now be redirected!</font>";

	}else{

		$_SESSION&#1111;'username'] = $_POST&#1111;'username'];
		$_SESSION&#1111;'passsword'] = $_POST&#1111;'password'];

		include("includes/connect.php");

		$sql = mysql_query("SELECT member_pass FROM members WHERE member_mail='{$_SESSION&#1111;'username']}'");
		$result = mysql_fetch_array($sql);
		$numrows = mysql_num_rows($sql);

		if($numrows != "0" && $_SESSION&#1111;'passsword'] == $result&#1111;"member_pass"]){
				$valid_user = 1;
			}else{
				$valid_user = 0;
		}

		if (!($valid_user)){

			session_unset();
			session_destroy();

			echo "<font face="verdana,arial,helvetica" size="2">Invalid Login! - you will now be redirected!</font>";
			echo "<meta http-equiv="refresh" content="3;URL=index.php">";

		}else{

			echo $_SESSION&#1111;'username'];
			echo "<a href="index.php">HERE</a>";
			
		}

	}
?>

--------------------------------------------------------
--------------------------------------------------------
--------------------------------------------------------

Here it is without all the HTML crap

INDEX.PHP

Code: Select all

<?
session_start();
?>

<? if(!isset($_SESSION&#1111;'username']) || !isset($_SESSION&#1111;'password'])){ ?>

<form action="account.php" method="post">
//form stuff
</form>

<? }else{ ?>

<font face="verdana,arial,helvetica" size="2">YOU ARE STILL LOGGED IN!</font>

<? } ?>
ACCOUNT.PHP

Code: Select all

<?
session_start();

if(empty($_POST&#1111;'username']) || empty($_POST&#1111;'password'])){

echo "ERROR!";

}else{

$_SESSION&#1111;'username'] = $_POST&#1111;'username'];
$_SESSION&#1111;'passsword'] = $_POST&#1111;'password'];

include("includes/connect.php");

$sql = mysql_query("SELECT member_pass FROM members WHERE member_mail='{$_SESSION&#1111;'username']}'");
$result = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);

if($numrows != "0" && $_SESSION&#1111;'passsword'] == $result&#1111;"member_pass"]){
$valid_user = 1;
}else{
$valid_user = 0;
}

if (!($valid_user)){

session_unset();
session_destroy();

echo "<font face="verdana,arial,helvetica" size="2">Invalid Login! - you will now be redirected!</font>";
echo "<meta http-equiv="refresh" content="3;URL=index.php">";

}else{

echo $_SESSION&#1111;'username'];
echo "<a href="index.php">GO BACK</a>";

}

}
?>

Thx for any help,

pHlange

Posted: Wed May 08, 2002 5:46 pm
by phice
One thing: Try refreshing the page, when you go back. Doing this will get a fresh version of the current website.
Second thing: Inside the last } else { section of ACCOUNT.PHP, be sure to add a session_register("username"); and session_register("password"); in side of it. You can change the content inside the ("");'s to specify on what you want the session to remember.