MySQL + PHP Account system problem

Community Forums/General Help/MySQL + PHP Account system problem

Arska(Posted 2011) [#1]
And another problem with MySQL.

I created fully working login and register system. But problem is that, when 2 accounts is logged in same time, it says that both are using same account. Here is some code to take look.

login.php
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" media="All" href="css/style.css" />
</head>
<body>

	<div id="wrap">

		<?php 

		mysql_connect("localhost:3306", "arska134", "********") or die(mysql_error());
		mysql_select_db("arska134") or die(mysql_error());

		if(isset($_POST['email']) && isset($_POST['password'])){


			// Haetaan tekstikenttien arvot
			$email = mysql_escape_string($_POST['email']);
			$password = md5($_POST['password']);

			// Yhdistetään MySQL tietokantaan
			$gUser = mysql_query("SELECT * FROM users2 WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error());
			$verify = mysql_num_rows($gUser);

			// Jos tiedot täsmäävät päästetään sisälle
			if($verify > 0){

				include 'userarea.php';
				
				
			// Jos tiedot ovat väärin
			}else{
				echo '<h3>Login Failed</h3>
		  		<p>Login details are incorrect.';
			}



		}else{
		?>
	
		<!-- Koodin HTML osuus alkaa. Lomake osa --> 
		<h3>Login</h3>
		<p>Please enter your login credentials. <a href="register.php">Register account</a> </p>
		<p><a href="search.php">Search account</a> </p>

		<form method="post" action="" >
			<fieldset>
				<label for="email">Email:</label><input type="text" name="email" value="" />
				<label for="password">Password:</label><input type="password" name="password" value="" />
				<input type="submit" value="Login" />
			</fieldset>
		</form>

		<?php
		}
		?>

	</div>

</body>
</html>


userarea.php (included in login.php)
<?php 

	// Haetaan tiedot käyttäjästä
	$data = mysql_query("SELECT * FROM users2") or die(mysql_error());
	$info = mysql_fetch_array( $data ); 

	echo "Current date and time: ".date("d.m.y : H:i:s", time());

	echo '<h3>Login Complete</h3>
	<p>Welcome!</p>';
	echo "Hello ".$info['name']." ".$info['surname'];
	
	if (filesize("activity.txt") > 1000) {
		unlink('activity.txt');
	}
	
	$activity = fopen("activity.txt", 'a');
	fwrite($activity, date("d.m.y : H:i:s", time())." - ".$info['name']." ".$info['surname']." has reguested refresh! ");
	fclose($activity);
	
	?>
	<p> </p>
	<?php

	echo "You are logged in with email ".$info['email'];	

	?>
	<p> </p>
	<h4>News</h4>
	<p> </p>
	<?php		


	$fh = fopen("news.txt", "r");

	while(!feof($fh)) { 
		$lines = trim(fgets($fh));
     		
		echo $lines;
     		break;
	}

	
	fclose($fh);

	
	?>
	<p> </p>
	<h4>Recent activity</h4>
	<p> </p>
	<?php
	
	$activity = fopen("activity.txt", "r");

	while(!feof($activity)) { 
		$actlines = trim(fgets($activity));
     		
		echo $actlines.'<br />';
     		break;
	}

	
	fclose($activity);	
	
	?>
	<p> </p>
	<?php
	
	echo "activity.txt ".filesize("activity.txt")." / 1000 bytes";
	
	?>
	<p> </p>
	<h4>Stream</h4>
	<p> </p>

	<form method="post" action="" >
			<fieldset>
				<label for="comments">Comment:</label><input type="text" name="comments" value="" />
				<input type="submit" value="Submit" />
			</fieldset>
	</form>

	<p> </p>
	<fieldset>
	<?php
	
	$stream = fopen("stream.txt", "r");

	while(!feof($stream)) { 
		$strelines = trim(fgets($stream));
     		
		echo $strelines;
     		break;
	}

	
	fclose($stream);	
	?>
	</fieldset>
	<?php
?>



and after this problem is solved someone can tell me why this "stream" thing isn't working. Submit button returns to login screen.


And here is screenshot of userarea


Arska(Posted 2011) [#2]
Some update to problem.

So i did some testing and i realize that it always takes first account from MySQL database.


CGV(Posted 2011) [#3]
It's been a while since I've done any PHP/MySQL but it looks to me that the problem is in the first line in 'userarea.php'.

$data = mysql_query("SELECT * FROM users2") or die(mysql_error());

You're grabbing the entire user2 table and your not iterating through it to find the user in question, so naturally you're just displaying the first entry.

Off hand I think you need to just use $verify in 'userarea,php' rather than re-fetch the data anew. Since you're including this file inside 'login.php' the $verify variable should be within it's scope.

Hope this helps, Bye!

Last edited 2011


Arska(Posted 2011) [#4]
Oh it was that simple. Now it's working just like i wanted. :)

Thanks CGV