Sessions not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Sessions not working?

Post by Cucumberdude »

Code: Select all

<?php
        //Retrieve form data
        $username = $_POST['username'];
        $password = $_POST['password'];
       
        //Access mySQL database to check the pass and user match
        $mySQL_password = "PASSWORD";
        $mySQL_username = "USERNAME";
        $mySQL_localhost = "HOST";
       
        $cnx = mysqli_connect($mySQL_localhost, $mySQL_username, $mySQL_password)
                or die("Error: could not connect to database!");
       
        $database = "sterli41_g4j";
       
        $dbselect = mysqli_select_db($cnx, $database)
                or die("Error: could not select database!");
               
        $query = "SELECT * FROM members";
       
        $result = mysqli_query($cnx, $query)
                or die(mySQL_error());
       
        $login = false;
       
        while ($row = mysql_fetch_array($result))
        {
                $pass = $row['password'];
                $user = $row['username'];
               
                if($user == $username)
                {
                        if($pass == $password)
                        {
                                $login = true;
                        }
                }
        }


        if ($login)
	{
		session_start();
		$_SESSION['loggedin'] = 1;
		header ('Location: http://www.xxx.com/loggedin.php');
	}
	else
	{
		header ('Location: http://www.xxx.com/login_failed.php');
	}
	
	mysqli_close($cnx);
		
?>
That is my login script. The user is then redirected to this:

Code: Select all

<html>
<head>
<title>Games for Junk</title>
<link rel=StyleSheet href="http://www.xxx.com/style.css" TYPE="text/css">
</head>
<body>
<table width="100%" border="0" cellpadding="10">
	<tr>
		<td colspan="2" height="100" valign="top">
			<?php
				 $data = file_get_contents('http://www.xxx.com/header.php');
				 echo($data);
			?>
		</td>
	</tr>
	<tr>
		<td class="nav" valign="top" width="150">
			<?php
				 $data = file_get_contents('http://www.xxx.com/navigation.php');
				 echo($data);
			?>
		</td>
		<td valign="top">
			
			<?php
				if($_SESSION['loggedin'] == 1)
				{
					echo("<h4>You are now logged in.</h4>");
				}
				else
				{
					echo("<h4>An error occurred.</h4>");
				}
			?>
</form> 
		</td> 
	</tr>
	<tr>
		<td colspan="2" height="50" align="center">
			<?php
				$data = file_get_contents('http://www.xxx.com/footer.php');
				echo($data);
			?>
		</td>
	</tr>
</table>
</body>
</html>
And for some reason it always says an error occurred...
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: Sessions not working?

Post by mellowman »

Im kinda confused by your code...Are u trying to basically make a login and a page which, if logged in will redirect you to one page and if not logged in will bring you to anther?
tomindo
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:22 am

Re: Sessions not working?

Post by tomindo »

what is exactly error message?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Sessions not working?

Post by social_experiment »

You have to call session_start() on any page where you wish to use session variables.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply