PHP login/registration help

louismoore18

New Member
Right im a bit of a beginner at this so please me gentle and try and help me as much as possible

Ive got a login & registration system currently in place which:
-users enter their details (they must check T&A before they can register)
-this data is then sent to a database
-A 'registration successful' is displayed and then gives them button to return home
-They can then log in and if sucessful displays 'login successful return home' or is not displays 'incorrect username/password' on another page
-It also then shows them logged in too

I have a couple of questions....
-Can anyone aid me on a system to send a email to confirm registration
-To get 'incorrect unsername/password' to display in an alert box
-if possible a forgotten password

Now before you say i have searched the net and found some tutorials but it doesnt really make sense to me. These toturials are starting from scratch and i want to add it to my current system.

PLEASE remember i am a BEGINNER

I have included some of my code

thanks

HTML:
<form method="post" name="form1" action="checklogin.php">&nbsp;Login: <input id="myusername" size="14" name="myusername"/> Password: <input id="mypassword" size="14" type="password" name="mypassword"/> <input value="Login" type="submit" name="mysubmit"/></form>New users? <a href="Untitled7.php">click here</a><a href="Untitled8.html">Forgotten password?<br/></a></font>

PHP:
<?php
error_reporting(-1); 
ini_set('display_errors', true); 

function exit_with_error($message){ 
  echo $message; 
  exit; 
} 

function escape($value){ 
  return mysql_real_escape_string($value); 
} 

if('POST' !== $_SERVER['REQUEST_METHOD']){ 
  exit_with_error('No form submission'); 
} 

$con = mysql_connect('localhost', 'root', ''); 

if(false === is_resource($con)){ 
  exit_with_error('Cannot connect: ' . mysql_error()); 
} 

if(!mysql_select_db('test')){ 
  exit_with_error('Cannot select database: ' . mysql_error()); 
} 

$sql = sprintf( 
  "SELECT username FROM members WHERE username = '%s' AND password = '%s' LIMIT 1;", 
  escape($_POST['myusername']), 
  sha1($_POST['mypassword']) 
); 

$res = mysql_query($sql); 

if(false === is_resource($res)){ 
  exit_with_error('Cannot execute query: ' . mysql_error()); 
} 

if(1 !== mysql_num_rows($res)){ 
  exit_with_error('Incorrect username/password'); 
} 

session_start(); 
$_SESSION['myusername'] = $_POST['myusername']; 
$_SESSION['mypassword'] = $_POST['mypassword']; 
header('location:login_success.php'); 
exit;


?>

PHP:
<?php
// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page.  
session_start();
if (!$_SESSION['myusername']) {  
header("location:main_login.php");
}
?>

<p align="center">
                <font size="5" face="Tempus Sans ITC">
                  <strong>Login Sucessfull!</strong> </font>
              </p>

PHP:
<?php 
error_reporting(-1); 
ini_set('display_errors', true); 

function exit_with_error($message){ 
  echo $message; 
  exit; 
} 

if($_POST['password'] !== $_POST['password2']){ 
  exit_with_error('Your passwords must match'); 
}

function escape($value){ 
  return mysql_real_escape_string($value); 
} 

if('POST' !== $_SERVER['REQUEST_METHOD']){ 
  exit_with_error('No form submission'); 
} 

$con = mysql_connect('localhost', 'root', '');


if(false === is_resource($con)){ 
  exit_with_error('Cannot connect: ' . mysql_error()); 
} 

if(!mysql_select_db('test')){ 
  exit_with_error('Cannot select database: ' . mysql_error()); 
} 

$sql = sprintf( 
  "INSERT INTO members (firstname, lastname, username, email, password, home_number, home_address1, home_address2, County, postcode ) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');", 
  escape($_POST['firstname']), 
  escape($_POST['lastname']), 
  escape($_POST['username']), 
  escape($_POST['email']), 
  sha1($_POST['password']),
  escape($_POST['house_number']),
  escape($_POST['home_address1']),
  escape($_POST['home_address2']),
  escape($_POST['County']),
  escape($_POST['postcode']) 
); 

if(!mysql_query($sql)){ 
  exit_with_error('Cannot save record: ' . mysql_error()); 
} 

echo 'Record saved!';

?>
 

nafirici

New Member
for the forgot password part, you could create a random hash token, store it in the database with an expiration. Then email the user a link with the token and allow them to reset there password.
 
Top