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" /> <br>
			Password: <input type="password" name="password" /> <br><br>
			<input type="submit" value="Login" />
			<input type="reset" value="Reset" /> <br>
		</form>

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

Dani AI

Generated

A blank white page almost always means a PHP error or a headers/redirect problem that your server is hiding. First step: make PHP show errors and check the WampServer/PHP error log so you can see the actual problem. Add these two lines while debugging (remove them in production):

ini_set('display_errors', 1);
error_reporting(E_ALL);

As already suggested, confirm the form actually sends POST and that the submit control has the name your script checks (so the isset($_POST['Login']) branch runs). A quick debug you can drop into the top of the login handler will show what the script receives:

var_dump($_POST);
exit;

Sessions and redirects need to be in the right order. Make sure the login script starts a session before you write session data, and that members.php starts a session before it reads it. Do not output HTML (or BOM/whitespace) before you try to send a Location header — if PHP reports "headers already sent" you'll know why. After sending a Location header, call exit/die to stop further output. Check members.php (as asked) to ensure it actually checks the session flag and redirects unauthenticated users back to the login page.

Longer-term: stop using the old mysql_ API and MD5. Use prepared statements (mysqli or PDO) and PHP’s password_hash()/password_verify() for password storage. See the manual for details: password_hash() manual and mysqli prepared statements quickstart. Also confirm your PHP version — mysql_ was removed in modern PHP.

Quick checklist: turn on errors, inspect logs, ensure form method/name, verify $_POST contents, start session before output, fix “headers already sent” issues, and migrate to mysqli/PDO + password_hash for production.

Recommended Answers

All 5 Replies

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 .

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...

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

and also

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

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

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...

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.