Hello seniors,
How are you….?
I need your help to remove little logical error form user registration form.
I have created a user login & registration form. It works fine when I try to reach that form by this method
First I put this address in address bar http://localhost/series/
After that I will select Registration Form Part/ through mouse
After that I will select register.php/ through mouse
When I reach to register.php there is a little registration form. Containing boxes with
1) username
2) password
3) password again
4) first name
5) sur name
first logical
know the first logical error is that when register.php will open then the , username, first name and sur name are already fill with this line.
username
<br /><b>Notice</b>: Undefined variable: username in <b>C:\xampp\htdocs\series\Registration Form Part\register.php</b> on line <b>48</b><br />
where I use this line of code in line 48
Username:<br> <input type ="text" name="username" value = "<?php echo $username; ?>"><br><br>
first name
<br /><b>Notice</b>: Undefined variable: firstname in <b>C:\xampp\htdocs\series\firstfile\DB\Registration Form Part\register.php</b> on line <b>51</b><br />
where I use this line of code in line 51
First Name:<br> <input type="text" name="firstname" value = "<?php echo $firstname; ?>"><br><br>
sur name
<br /><b>Notice</b>: Undefined variable: surname in <b>C:\xampp\htdocs\series\firstfile\DB\Registration Form Part\register.php</b> on line <b>52</b><br />
where I use this line of code in line 51
Sur Name:<br> <input type="text" name="surname" value = "<?php echo $surname; ?>"><br><br>
Purpose of that code
Actually, I use php code in VALUE of html FORM so that user will have no need to retype again and again in case of any mistake.
second logical error
Now, the second logical error.
When I reach to register.php, this page will appear ok with first logical error as I discuss above. And the link appear on address bar is http://localhost/series//Registration%20Form%20Part/register.php
Now if I copy this address form address bar and put in to a new window’s address bar then register.php page will appear with this error.
Notice: Undefined index: HTTP_REFERER in C:\xampp\htdocs\series\firstfile\DB\Registration Form Part\core.inc.php on line 6
Here is all code of registration form
Please check it and tell me how I can improve this one and how I can remove these logical errors.
Thaks
registration form page, Named: register.php
<?php
require 'core.inc.php';
require 'connect.inc.php';
if(!loggedin()){
if(
isset($_POST['username']) &&
isset($_POST['password']) &&
isset($_POST['password_again']) &&
isset($_POST['firstname']) &&
isset($_POST['surname'])
){
$username = $_POST['username'];
$password = $_POST['password'];
$password_hash = md5($password);
$password_again = $_POST['password_again'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
if(
!empty ($username) &&
!empty ($password) &&
!empty ($password_again) &&
!empty ($firstname) &&
!empty ($surname)
){
if($password == $password_again){
$query = "SELECT username FROM users WHERE username = '$username'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)==1){
echo 'Sorry..! This user name '.$username.' already exist. <br>Please try with any other.';
}else{
$query = "INSERT INTO users VALUES ('', '$username', '$password_hash', '$firstname', '$surname')";
if($query_run = mysql_query($query)){
header('Location: register_success.php');
}
}
}else{
echo 'Password is not matching.<br> Please enter again same password.';
}
}else{
echo 'All fields are required. Thanks';
}
}
?>
<form action="register.php" method="POST">
Username:<br> <input type ="text" name="username" value = "<?php echo $username; ?>"><br><br>
Password:<br> <input type ="password" name="password"><br><br>
Password Again: <br> <input type="password" name="password_again"><br><br>
First Name:<br> <input type="text" name="firstname" value = "<?php echo $firstname; ?>"><br><br>
Sur Name:<br> <input type="text" name="surname" value = "<?php echo $surname; ?>"><br><br>
<input type="submit" value="Register" >
</form>
<?php
}else if(loggedin()){
echo 'You\'re logged in.';
}
?>
Another page Named: core.inc.php
<?php
ob_start(); //ob_start should start before use of header function, which is to redirect our user back to index page when they login correctly
session_start(); //session_start should initialize before we use session
$current_file = $_SERVER['SCRIPT_NAME']; //{'SCRIPT_NAME'} tell us what is current page
$http_referer = $_SERVER['HTTP_REFERER'];
//function to check weather session is empty or not?
function loggedin(){
if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
return true;
}else{
return false;
}
}
function getUserField($field){
$query = "SELECT $field FROM users WHERE id = '".$_SESSION['user_id']."' ";
if($query_run = mysql_query($query)){
if($query_result = mysql_result($query_run, 0 , $field)){
return $query_result;
}else{
echo 'Love.';
}
}
}
?>
page connect to database Named: connect.inc.php
<?php //To connect script with database
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'a_database';
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || ! mysql_select_db($mysql_db)){
die(mysql_error());
}
?>
user login form page Named: loginform.inc.php
<?php
//this should not userequire 'core.inc.php';
require_once('core.inc.php');
//cheching login form is empty or not? And then form data send to database server to compare weather data is valid or not? After that start session if data is valid.
if(isset($_POST['username']) && isset($_POST['password'])){
$username=$_POST['username'];
$password=$_POST['password'];
$password_hash = md5($password); //converting data to md5
if(!empty($username) && ! empty($password)){
//here we select id form table rather username, so that we can access whole data of a user who logged in
$query = "SELECT id FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password_hash)."'";
if($query_run = mysql_query($query)){
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows==0){
echo 'Invalid';
}else if($query_num_rows ==1){
//mysql_result have take these three arguments,
//$query, The result resource that is being evaluated. This result comes from a call to mysql_query().
//Then Row(0) The row number from the result that's being retrieved. Row numbers start at 0.
//third one is "field" The name or offset of the field being retrieved. It can be the field's offset, the field's name, or the field's table dot field name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), use the alias instead of the column name. If undefined, the first field is retrieved.
$user_id = mysql_result($query_run, 0, 'id');
echo $user_id;
$_SESSION['user_id']=$user_id; //starting session and assigning user id to recognize user
header('Location: main.php'); //header return user to main.php page
}
}
}else{
echo 'You must enter a username and password.';
}
}
?>
<form action="<?php echo $current_file; //variable recognize current page of website ?>" method="POST">
Usrename: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Log in">
</form>
Log out page Named: logout.php
<?php
require 'core.inc.php';
session_destroy();
header('Location:'.$http_referer);
?>
main or index page Named: main.php
<?php
require 'core.inc.php'; //calling other page which has some function of login part
require 'connect.inc.php'; //calling other page which has some function of login part
//loggedin function is declare in core.inc.php page to check that weather session is exist or not and session is continue or not?
if(loggedin()){
$firstName = getUserField('firstname');
$surName = getUserField('surname');
echo 'Welcome '.$firstName.' '.$surName.'...!<br> You are login. <a href = "logout.php">LogOut</a><br>';
}else{
include 'loginform.inc.php';
}
?>
if register successful move to this page Named: register_success.php
You are register