954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Login Script

Ok, so l am new to php ... sort of, now l have managed to throw this together:

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test_database", $con);

	if(isset($_POST['Login']))
	{
		if($_POST['username']!='' && $_POST['password']!='')
		{
			//Use the input username and password and check against 'users' table
			$query = mysql_query('SELECT ID, username, Active FROM persons WHERE username = "'.mysql_real_escape_string($_POST['username']).'" AND password = "'.mysql_real_escape_string(md5($_POST['password'])).'"');
			
			if(mysql_num_rows($query) == 1)
			{
				$row = mysql_fetch_assoc($query);
				if($row['Active'] == 1)
				{
					$_SESSION['user_id'] = $row['ID'];
					$_SESSION['logged_in'] = TRUE;
					header("Location: members.php");
				}
				else {
					$error = 'Your membership was not activated. Please open the email that we sent and click on the activation link';
				}
			}
			else {		
				$error = 'Login failed !';		
			}
		}
		else {
			$error = 'Please enter both your username and password to access your account';
		}
	}
?>


Here is my html:

<form action="login.php" method="get">
			Username: <input type="text" name="username" /> 
			Password: <input type="password" name="password" /> 
			<input type="submit" value="Login" />
			<input type="reset" value="Reset" /> 
		</form>


Now, this is not working, when l try it it displays a blank white page, anyone know what l have done wrong?

dlannetts
Junior Poster in Training
84 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
 
ob_start();
session_start();

fix above code at top of page
and replace

mysql_fetch_assoc($query);

with

mysql_fetch_array($query);

and in form

<form action="login.php" method="post">

replace method="get" with method="post " as like above.
try it .

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

mmm, this didn't work, still displays a white page, due to my script it should open the file members.php if it logged in successfully shouldn't it? before it was adding extra stuff into my url bar too, what you have done has gotten rid of that, so that is good l guess...

Added a picture of the wampserver screen db, click image for larger view...

Attachments WampServer.JPG 15.73KB
dlannetts
Junior Poster in Training
84 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
 

Does your page members.php have some code that checks the session variable is set? If so can you post that code?

Hangfire
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 14
Solved Threads: 12
 

and also

<input type="submit"  name="Login"value="Login" />

fix it. you forgot to write name="Login" in submit botton.

rajabhaskar525
Junior Poster
179 posts since Nov 2009
Reputation Points: 12
Solved Threads: 27
 

and also

<input type="submit"  name="Login"value="Login" />

fix it. you forgot to write name="Login" in submit botton.

Yep, done this, forgot about that, this is my members.php file:

<?php

session_start();

?>

<html>
<body>
help...


AZnythiong wrong here? thanks for everyones help so far, yous have been great...

dlannetts
Junior Poster in Training
84 posts since Jan 2010
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You