Hey guys im struggling to pass a session in php, i cant quite figure out why though.
My friend used the exact same code as me but it doesnt want to work.

This code is the database checker (which seems to work) maybe im over looking some thing??

<?php
session_start();
$host="localhost"; //host name is localhost
$tbl_select="tbluser"; // Table name

// Connect to server and select databse.
$db=mysql_connect('localhost', 'us8', 's8');
$db_selected=mysql_select_db('dbs8', $db);
$myUserName=$_POST['username'];//store the username from the form in the variable myusername
$myPassword=$_POST['password'];//store the password from the form in the variable mypassword
// To protect MySQL injection....
$myUserName = stripslashes($myUserName);
$myPassword = stripslashes($myPassword);
$myUserName = mysql_real_escape_string($myUserName);
$myPassword = mysql_real_escape_string($myPassword);
//select user
$sql="SELECT * FROM $tbl_select WHERE username='$myUserName' and password='$myPassword'"; //get the username and password form the table user
$result=mysql_query($sql); //store the username and password in variable result
$count = mysql_num_rows($result); // count the number of rows in the result
if ($count==1) // if there is 1 row
{
 $_SESSION['myUserName']=$myUserName;
 $_SESSION['loggedIn']="true"; //they are logged in
// setcookie("myUserCookie", $myUserName, time()+3600); set a cookie called myUserCookie with the value of username which expires in an hour
 header('Location: test1.php'); //they are redirected back to the homepage
}
else
{
 /*session_unset(); //get rid of session values
 session_destroy(); //destroy the session
 header('Location:index.php'); //redirect back to the home page*/
	echo "Nah";
}
?>

and this is the code snippet that im having a problem with (it doesnt want to take the login area away if the user is logged in)

<?php 
										 //checking whether logged in...
										 
										if (isset($_SESSION['loggedIn']))
										  {
										  echo "<table align='right'><form name='logOut' method='post' action='logout.php'><tr><td>Hi S11 you are currently logged in</td></tr><tr><td>1 is your user ID</td></tr><td><input type='submit' name='submit' value='Log Out'></td></form></table>   </div><!--closes login-->";
										  }
										else if (!isset($_SESSION['loggedIn']))
										{
											echo "<table align='right'>";
											echo  "<form name='login' method='post' action='test.php'><tr>";
											echo      "<td>User Name</td>";
											echo      "<td><input name='username' type='text' id='username' /></td>";
											echo      "<td>Password</td>";
											echo     "<td><input name='password' type='password' id='password' /></td>";
											echo      "<td colspan='2'><input type='submit' name='Submit' value='Login' /></td>";
											echo    "</tr>";
											echo  "</form>";
											echo "</table>";
										}
?>

Recommended Answers

All 13 Replies

Kindly check whether $_SESSION session is flushed away during logout.
And what is exact steps and which is not coming in your page bcz of session error?

well the thing is it registers that its logged in... and the logout does work so yea it is flushed out, however why it doesnt alow for it to be registered as set when passing back to index.php is passed me? i got no clue as to why its not working

Is these two different php pages ?
Post your complete code to review.

the issue is it only shows login (dunno if i cleared that up) it doesnt show log out it just keeps showing the login (so the else if statement)

no matter which way i try to edit the code

you sure you want my full code?? it quite a bit (yea they are 2 diff pages)

If you are checking like this:
if (isset($_SESSION))

then assign values as :
$_SESSION == true;
// no qoutes e.g. "true"(line # 23 in ur first code)

Change code and then try it.

nope still doesnt

ok heres the pages as per wanted

login check

<?php
session_start();
$host="localhost"; //host name is localhost
$tbl_select="tbluser"; // Table name

// Connect to server and select databse.
$db=mysql_connect('localhost', 'us8', 's8');
$db_selected=mysql_select_db('dbs8', $db);
$myUserName=$_POST['username'];//store the username from the form in the variable myusername
$myPassword=$_POST['password'];//store the password from the form in the variable mypassword
// To protect MySQL injection....
$myUserName = stripslashes($myUserName);
$myPassword = stripslashes($myPassword);
$myUserName = mysql_real_escape_string($myUserName);
$myPassword = mysql_real_escape_string($myPassword);
//select user
$sql="SELECT * FROM $tbl_select WHERE username='$myUserName' and password='$myPassword'"; //get the username and password form the table user
$result=mysql_query($sql); //store the username and password in variable result
$count = mysql_num_rows($result); // count the number of rows in the result
if ($count==1) // if there is 1 row
{
 $_SESSION['myUserName']=$myUserName;
 $_SESSION['loggedIn']==true; //they are logged in
 setcookie("myUserCookie", $myUserName, time()+3600); //set a cookie called myUserCookie with the value of username which expires in an hour
    header('Location:http://virtual.weltec.ac.nz/s8'); //they are redirected back to the homepage
}
else
{
 session_unset(); //get rid of session values
 session_destroy(); //destroy the session
    header('Location:login.php'); //redirect back to the home page
}
?>

index (snipet)

<td style="background-repeat: no-repeat; vertical-align:top" background="images/Darkroom_spotlight.jpg" ><div align="right"><table height="50"> <tr> <td> 
					<?php 
										 //checking whether logged in...
										 
										 if (isset($_SESSION['loggedIn']))
										 {
										  echo "<table align='right'>";
										  echo "<form name='logOut' method='post' action='logout.php'><tr>";
										  echo "<td>Hi S11 you are currently logged in</td></tr>";
										  echo "<tr><td>1 is your user ID</td></tr>";
										  echo "<td><input type='submit' name='submit' value='Log Out'></td>";
										  echo "</form>";
										  echo "</table>";
										  echo "</div>";

										 }
										 else if (!isset($_SESSION['loggedIn']))
										 {
										  echo "<table align='right'>";
										  echo "<form name='login' method='post' action='login_check.php'><tr>";

										  echo "<td>User Name</td>";
										  echo "<td><input name='username' type='text' id='username' /></td>";
										  echo "<td>Password</td>";
										  echo "<td><input name='password' type='password' id='password' /></td>";
										  echo "<td colspan='2'><input type='submit' name='Submit' value='Login' /></td>";

										  echo "</tr>";
										  echo "</form>";
										  echo "</table>";
										 }
?></td>

website to check what i want is this
http://virtual.weltec.ac.nz/s8

o and sory my code is sloppy haha

there are lot a need to learn about PHP.Please can any body help review my blog ro me.www.danielessiet.com

cum on buddy my post :(

Firstly wana give you some notes,, if u like !!!
- Try html code out side of php tags.It will help you always.
- Form tag always must be above table tag
- Some nice tabs and intending :)

i have changed if condition here. check it.

<?php 										 
	 if ( isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn']) )
	 {
	 ?>
		  <form name='logOut' method='post' action='logout.php'>
		  <table align='right'>
			  <tr>
				<td>Hi S11 you are currently logged in</td>
			  </tr>
		  <tr><td>1 is your user ID</td></tr>
		  <td><input type='submit' name='submit' value='Log Out'></td>											  
		  </table>
		  </form>
		<!-- </div>  Tag breaking -->
	 <?
	 }
	 else
	 {
	 ?>
	  <form name='login' method='post' action='login_check.php'>
	  <table align='right'>										  
	  <tr>	
		  <td>User Name</td>
		  <td><input name='username' type='text' id='username' /></td>
		  <td>Password</td>
		  <td><input name='password' type='password' id='password' /></td>
		  <td colspan='2'><input type='submit' name='Submit' value='Login' /></td>
	  </tr>											  
	  </table>
	  </form>
	  <?
	 }
	 ?>

but i tried that it makes the code visible in bothe circumstances? but i will try it again, and let you know thanks for your input

nope didnt work :(

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.