Vitrex
Moderator
Heya , Emudevs !
Today i'm gonna show you how to make the simple register/login system with php language and mysql database. it will be not object programming , this tutorial continues my previous it's for newbies that just started.
Okay what we need for that part REGISTER ?
Open up your database with whatever program u use navicat,heidysql or just with PHP MYADMIN (highly recommend that way).
And lets create our table users with columns :
Now when we have our table created we need to create few files in our website folder :
index.php
mysql.php
register.php
login.php
Open up your mysql.php
And write there variables to connect for database like :
now time to learn something new, mysql_connect and mysql_select_db (of course like all we know that newest php do not support mysql functions anymore, but most of web services still use old php OR let you to install it by yourself on VPS / VDS.
Now lets connect !
Okay what we done here? we creating a variable connect and call our mysql_connect funtion , then we call function to select database in our mysql and this one " Or die(mysql_error()); " do that : if connection fails print the error to know why connection failed.
Move on our index.php
it looks empty, right? time to fill some info .
okay okay, stop now lets comment out all things !
we get $stage value from address bar login or register and check if the $stage was set and what value it have, if register we include our register.php file
if it have login value we include our login.php file otherwise if $stage was not set we just output our links to register or login.
Move on our register.php file !
lets create the form first of all !
ok we have simple html form with our all info we wanna get from users to register. now time add php code.
The full file will be look like :
now go to our login.php file !
and create the html form to log in !
Now come back to your index.php file it must look like :
Lets edit it !
add at the beginins Session_start();
Then find this :
your file must look like :
Today i'm gonna show you how to make the simple register/login system with php language and mysql database. it will be not object programming , this tutorial continues my previous it's for newbies that just started.
Okay what we need for that part REGISTER ?
Open up your database with whatever program u use navicat,heidysql or just with PHP MYADMIN (highly recommend that way).
And lets create our table users with columns :
Code:
id(int , 255) - don't forget to make this one unique and AI - auto incriment !
username(varchar,255)
password(varchar,255)
email(varchar,255)
signature(text)
date(varchar,255)
index.php
mysql.php
register.php
login.php
Open up your mysql.php
And write there variables to connect for database like :
Code:
// <- database settings
$host = " ";
$database = " ";
$username = " ";
$password = " ";
// ->
Now lets connect !
Code:
$connect = mysql_connect($host,$username,$password)or die(mysql_error());
mysql_select_db($database,$connect)or die(mysql_error());
Move on our index.php
it looks empty, right? time to fill some info .
Code:
<?php
// lets include our mysql connection file
include 'mysql.php';
$stage = mysql_real_escape_string(isset($_GET['stage'])) ? $_GET['stage'] : Null; // just clear our string and get index.php?stage= info
if(isset($stage)){
switch($stage){
case: $stage == "register"
include 'register.php';
Break;
case: $stage == "login"
include 'login.php';
Break;
Default:
Break;
}
} else {
echo "<a href = '?stage=register'>Click me to register</a>";
echo "<a href = '?stage=login'>Click me to login</a>";
}
// lets close connection to avoid random queries we don't need
mysql_close($connection);
?>
okay okay, stop now lets comment out all things !
we get $stage value from address bar login or register and check if the $stage was set and what value it have, if register we include our register.php file
if it have login value we include our login.php file otherwise if $stage was not set we just output our links to register or login.
Move on our register.php file !
lets create the form first of all !
Code:
<form action = '' method = 'POST'>
Etner your username : <input type = 'text' name = 'username' value = ''/> <br />
Etner your password : <input type = 'password' name = 'password' value = ''/> <br />
Confirm your pasword : <input type = 'password' name = 'password_confirm' value = ''/> <br />
Etner your Email : <input type = 'text' name = 'email' value = ''/> <br />
<input type = 'submit' name = 'submit' value = 'register' />
</form>
ok we have simple html form with our all info we wanna get from users to register. now time add php code.
Code:
<?php
if(isset($_POST['submit'])){ // check if button submit was pressed
// --> extract data from our html form and clear strings from shit
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$password_confirm = mysql_real_escape_string($_POST['password_confirm']);
$email = mysql_real_escape_string($_POST['email']);
// < --
if(!empty($username) || !empty($password)) || !empty($password_confirm) || !empty($email) { // check if there no empty inputs
$username_check = mysql_query("SELECT username FROM users WHERE username = '".$username."'"); // select username from database like user entered in our username input
if($usernam_check > 0) { // check the value of our query if there no that username value will be 0 if bigger then send users error with message !
echo "Sorry username already taken, please enter other !";
} else {
if($password == $password_confirm) {
if(filter_var('$email',FILTER_VALIDATE_EMAIL)) {
// now we filtered all out we have no taken username matching passwords and valid email !
// time to insert our user data !
$password = SHA1($password); // user SHA 1 encrypt for password to hide it.
$date = date('Y-M-D'); // set the variable $date to today's year month and day
mysql_query("INSERT INTO `users` VALUES(NULL,'$username','$password','$email','$date');");
} else {
echo "Email is wrong !";
}
} else {
echo "Passwords doesn't match please check it !";
}
}
}
}
?>
The full file will be look like :
Code:
<?php
if(isset($_POST['submit'])){ // check if button submit was pressed
// --> extract data from our html form and clear strings from shit
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$password_confirm = mysql_real_escape_string($_POST['password_confirm']);
$email = mysql_real_escape_string($_POST['email']);
// < --
if(!empty($username) || !empty($password)) || !empty($password_confirm) || !empty($email) { // check if there no empty inputs
$username_check = mysql_query("SELECT username FROM users WHERE username = '".$username."'"); // select username from database like user entered in our username input
if($usernam_check > 0) { // check the value of our query if there no that username value will be 0 if bigger then send users error with message !
echo "Sorry username already taken, please enter other !";
} else {
if($password == $password_confirm) {
if(filter_var('$email',FILTER_VALIDATE_EMAIL)) {
// now we filtered all out we have no taken username matching passwords and valid email !
// time to insert our user data !
$password = SHA1($password); // user SHA 1 encrypt for password to hide it.
$date = date('Y-M-D'); // set the variable $date to today's year month and day
mysql_query("INSERT INTO `users` VALUES(NULL,'$username','$password','$email','$date');");
} else {
echo "Email is wrong !";
}
} else {
echo "Passwords doesn't match please check it !";
}
}
}
} else {
?>
<form action = '' method = 'POST'>
Etner your username : <input type = 'text' name = 'username' value = ''/> <br />
Etner your password : <input type = 'password' name = 'password' value = ''/> <br />
Confirm your pasword : <input type = 'password' name = 'password_confirm' value = ''/> <br />
Etner your Email : <input type = 'text' name = 'email' value = ''/> <br />
<input type = 'submit' name = 'submit' value = 'register' />
</form>
<?php
}
?>
now go to our login.php file !
and create the html form to log in !
Code:
<?php
if(isset($_POST['submit'])) { // again check if sumbit was pressed
$username = mysql_real_escape_string($_POST['username']); // grab data from inputs
$password = SHA1(mysql_real_escape_string($_POST['password'])); // grab data from inputs and encrypt password
$fetch = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE username = '".$username."'")); // grab data from database and put it in array
if($username == $fetch['username'] || $password == $fetch['password']) { // check if entered info the same are in databse.
$_SESSION['user_id'] = $fetch['id']; // set session user_id equal user's id !
Header("Location: Index.php"); // redirect us back to noramal index.php without stage
Exit; // just exit the header.
} else {
echo "You entered wrong information !";
}
} else {
?>
<form action = '' method = 'POST'>
Etner your username : <input type = 'text' name = 'username' value = ''/> <br />
Etner your password : <input type = 'password' name = 'password' value = ''/> <br />
<input type = 'submit' name = 'submit' value = 'register' />
</form>
<?php
}
?>
Now come back to your index.php file it must look like :
Code:
<?php
// lets include our mysql connection file
include 'mysql.php';
$stage = mysql_real_escape_string(isset($_GET['stage'])) ? $_GET['stage'] : Null; // just clear our string and get index.php?stage= info
if(isset($stage)){
switch($stage){
case: $stage == "register"
include 'register.php';
Break;
case: $stage == "login"
include 'login.php';
Break;
Default:
Break;
}
} else {
echo "<a href = '?stage=register'>Click me to register</a>";
echo "<a href = '?stage=login'>Click me to login</a>";
}
// lets close connection to avoid random queries we don't need
mysql_close($connection);
?>
Lets edit it !
add at the beginins Session_start();
Then find this :
Code:
before
this :
if(isset($stage)){
add this :
if(isset($_SESSION['user_id'])) {
} else {
and after this :
echo "<a href = '?stage=register'>Click me to register</a>";
echo "<a href = '?stage=login'>Click me to login</a>";
}
add this :
}
Code:
<?php
Session_start();
// lets include our mysql connection file
include 'mysql.php';
$stage = mysql_real_escape_string(isset($_GET['stage'])) ? $_GET['stage'] : Null; // just clear our string and get index.php?stage= info
if(isset($_SESSION['user_id'])) { // check if our session set for users ?
$user = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['id']."'")); // select all our user info by him id !
if(isset($stage) || $stage == "logout") { // check if stage is for logout or no?
Session_destroy('user_id'); // destroy our session !
Header("Location: index.php"); // refresh page
Exit;
}
echo "Hello,".$user['username']."<br />";
echo "Your signature : <br />";
echo $user['signature'],"<br />";
echo "you don't want be logged in? then press <a href = '?stage=logout'>Click me then !</a>"; // logout button
} else {
if(isset($stage)){
switch($stage){
case: $stage == "register"
include 'register.php';
Break;
case: $stage == "login"
include 'login.php';
Break;
Default:
Break;
}
} else {
echo "<a href = '?stage=register'>Click me to register</a>";
echo "<a href = '?stage=login'>Click me to login</a>";
}
}
// lets close connection to avoid random queries we don't need
mysql_close($connection);
?>
Last edited: