Hey everyone

I've made this website and want to use PHP to allow users to register and login. I've created a database and connected to it, i've also created the table to store data from the registration page.

I've tested it and PHP for the registration page seems to be working, when a enter my data and click submit and page is then returned saying "1 record added". The problem i'm having is with the login page. I enter the same details used to register and click submit. Instead of logining me in it takes ages to loads and then IE just says "page cannot be found"

Can anyone please help me figure out why it isn't working?

All help is greatly appreciated

Heres my coding

insert.php (registration page)

<?php 
$con = mysql_connect("localhost","u08103765","aliasurn"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("u08103765", $con); 
$sql="INSERT INTO member (firstname, surname, email, password, address1, address2, address3, postcode, telephonenumber, mobilenumber) 
VALUES 
('$_POST[firstname]','$_POST[surname]','$_POST[email]','$_POST[password]','$_POST[address1]','$_POST[address2]','$_POST[address3]','$_POST[postcode]','$_POST[telephonenumber]','$_POST[mobilenumber]')"; 
if (!mysql_query($sql,$con)) //we call the function 
{ // inside IF statement 
die('Error: ' . mysql_error()); // to check and 
} 
else 
{ 
echo ("1 record added"); // get some feedback 
} 
mysql_close($con) 
?>

that works fine and saves the data correctly in the table

login.php - this is the code i use to try and login but everytime i try it fails..

<?php $host="localhost"; // Host name    
   
$dbusername="u08103765"; // Mysql username    
   
$dbpassword="aliasurn"; // Mysql password    
   
$db_name="u08103765"; // Database name    
   
$tbl="member"; // Table name    
   
// This connects to server and then selects the members databse.    
   
mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect");    
   
mysql_select_db("$db_name")or die("cannot select DB");    
   
// Assign the username and password from the form to variables.    
   
$username=$_POST['email'];    
   
$password=$_POST['password'];    
   
$sql="SELECT * FROM $tbl WHERE username='$email' and password='$password'";    
   
$result=mysql_query($sql);    
   
// This counts to see how many rows were found, there should be no more than 1    
   
$count=mysql_num_rows($result);    
   
// If result matched $email and $pass, table row must be 1    
   
if($count==1){    
   
// Register $email, and redirect to file "login_success.php"    
session_start();    
$_SESSION["logged"] = 1;    
header("location:index.php");    
}    
else {    
$_SESSION["logged"] = 0;    
header("location:login.php");    
   
}    
   
?>

login_success.php - this is the page i want the logged in member to be redirected to

// Check to see if session is not registered, if it is not redirect the user back to the notloggedin.php file.    
   
// Put this code in first line of every web page that you only want logged-in users to see.    
   
<?php session_start();    
if($_SESSION['logged'] != 1){ header("location:login.php"); }    
   
?>   

<html> 

<head> 

<meta http-equiv="REFRESH" content="5;url=http:"...members.html"> 

</head> 
</html>

I know ...members.html isnt a proper link but i still need to create the page so i can fill that in later

Hope you can help me

Dani AI

Generated

Quick diagnosis: the login failures are almost always caused by mismatched names/variables or a session/redirect bug rather than MySQL being down. The registration code stores an email column but the login query is checking username and even uses an undefined variable — that will prevent any match. Also session handling is inconsistent: session_start() only runs in the success branch, so the failure branch writes $_SESSION without a started session. The meta-refresh in your success page looks malformed and can break redirects. was right to suggest echoing the row count — that will immediately show whether the query returns anything.

Quick debugging steps to run right now:

  • Turn on errors at the top of the script and dump incoming form data:
ini_set('display_errors', 1);
error_reporting(E_ALL);
var_dump($_POST);
  • Echo the SQL you build (or better, use prepared statements) and check the database directly (phpMyAdmin or the mysql CLI).
  • Check the webserver/PHP error log for fatal errors or parse errors.
  • Confirm your form uses method="post" and the input name attributes match what the script expects.

Secure, modern login flow (use this instead of string-building queries and plaintext passwords): store passwords with password_hash() at registration and verify with password_verify() at login. Example using PDO and prepared statements:

<?php
$email = $_POST['email'] ?? '';
$pwd   = $_POST['password'] ?? '';

$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','dbuser','dbpass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT id, password_hash FROM member WHERE email = ? LIMIT 1');
$stmt->execute([$email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user && password_verify($pwd, $user['password_hash'])) {
    session_start();
    $_SESSION['user_id'] = $user['id'];
    header('Location: index.php');
    exit;
}
header('Location: login.php?error=1');
exit;
?>

Action checklist for : fix the column/variable mismatch (email vs username), make sure session_start() runs before any $_SESSION writes, call exit; after header() redirects, stop posting DB credentials publicly (good point ), and migrate to prepared statements + password_hash() for safety. Read the docs for prepared statements and password handling: PDO prepared statements and password_hash/password_verify.

Recommended Answers

All 3 Replies

did you echo the count and see if it is getting a value ?

first off i would not show your username and pasword for your
db security issue

did you echo the count and see if it is getting a value ?

How do i do that? (sorry im not very good with php! i followed a tutorial for most of this)

Cheers

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.