Skip to content Skip to sidebar Skip to footer

Show Separate Uploads as Individual Lists Php Mysql

In social networking websites like Facebook, Instagram, etc, the username and profile pic of the user that has logged in gets displayed in the header of the website, and that header remains constant, irrespective of the webpage the user has opened. Such functionality tin be created by using the session variables.
Session variables be only while the user's session is agile. After the session is consummate, the session variables get destroyed. These are unique for each visitor and are generally used to store user-specific data such as the username, contour picture etc, once the user logs in.
The session variables are used to display logged in user information in PHP.
Projection Explanation and Code:
This is a simple registration system. The annals.php page asks for the desired username, email, and password of the user, and then sends the entered data into the database, once the submit push button is clicked. After this, the user is redirected to the index.php page where a welcome message and the username of the logged-in user is displayed.
The first step is to create a database, and and so a table inside it. The database is named 'registration', and the table is named 'users'. The 'users' table will contain iv fields.

  1. id – chief key – auto increase
  2. username – varchar(100)
  3. email – varchar(100)
  4. password – varchar(100)
    The 'id' will be the principal central, it means that it will exist unique for every registered user. It will besides auto-increment for every new registration. The information type for username, electronic mail and password volition be varchar. The size can exist adapted as per the requirement still, 100 is sufficient.
    SQL code for the table:

sql

CREATE Tabular array `users` (

`id` int (11) Non Nil AUTO_INCREMENT PRIMARY Cardinal ,

`username` varchar (100) Non NULL ,

`email` varchar (100) Not Nix ,

` password ` varchar (100) Non Zip

)

phpMyAdmin afterwards the database and table creation

Project folder, containing the necessary files

error.php

html

<? php if (count($errors) > 0) : ?>

< div class = "mistake" >

<? php foreach ($errors as $error) : ?>

< p ><? php echo $fault ?></ p >

<? php endforeach ?>

</ div >

<? php endif ?>

Explanation: The fault.php file is responsible for holding the error messages of the system. Suppose the user enters the wrong username and password combination, then in such cases, the error messages will be stored in the $error variable, which will and so be displayed to the user using 'echo; role of PHP.
server.php

php

<?php

session_start();

$username = "" ;

$email = "" ;

$errors = array ();

$_SESSION [ 'success' ] = "" ;

$db = mysqli_connect( 'localhost' , 'root' , '' , 'registration' );

if (isset( $_POST [ 'reg_user' ])) {

$username = mysqli_real_escape_string( $db , $_POST [ 'username' ]);

$electronic mail = mysqli_real_escape_string( $db , $_POST [ 'email' ]);

$password_1 = mysqli_real_escape_string( $db , $_POST [ 'password_1' ]);

$password_2 = mysqli_real_escape_string( $db , $_POST [ 'password_2' ]);

if ( empty ( $username )) { array_push ( $errors , "Username is required" ); }

if ( empty ( $electronic mail )) { array_push ( $errors , "Email is required" ); }

if ( empty ( $password_1 )) { array_push ( $errors , "Password is required" ); }

if ( $password_1 != $password_2 ) {

array_push ( $errors , "The two passwords practise not match" );

}

if ( count ( $errors ) == 0) {

$countersign = md5( $password_1 );

$query = "INSERT INTO users (username, email, password)

VALUES( '$username' , '$email' , '$password' )";

mysqli_query( $db , $query );

$_SESSION [ 'username' ] = $username ;

$_SESSION [ 'success' ] = "Yous have logged in" ;

header( 'location: alphabetize.php' );

}

}

if (isset( $_POST [ 'login_user' ])) {

$username = mysqli_real_escape_string( $db , $_POST [ 'username' ]);

$password = mysqli_real_escape_string( $db , $_POST [ 'countersign' ]);

if ( empty ( $username )) {

array_push ( $errors , "Username is required" );

}

if ( empty ( $countersign )) {

array_push ( $errors , "Countersign is required" );

}

if ( count ( $errors ) == 0) {

$password = md5( $countersign );

$query = "SELECT * FROM users WHERE username=

'$username' AND password= '$password' ";

$results = mysqli_query( $db , $query );

if (mysqli_num_rows( $results ) == one) {

$_SESSION [ 'username' ] = $username ;

$_SESSION [ 'success' ] = "You have logged in!" ;

header( 'location: index.php' );

}

else {

array_push ( $errors , "Username or password incorrect" );

}

}

}

?>

Explanation: The session is started using session_start() method. Subsequently that, the variables are declared and an fault array is created. Information technology volition store all the error messages. The server.php page is and then connected to the 'registration' database created earlier. Afterwards the user clicks the 'register' button on the register.php button, the data entered is sent to the database, and this completes a new registration. However, grade validation is done earlier that to make sure that the user is filling the form correctly. All the fields are required and cannot be left blank.
Line 18 – 21: mysqli_real_escape_string escapes the special characters before sending the data to the database. This is essential for database security from SQL injections.
Line 25 – 27: These lines makes sure that the user is filling all the input boxes, and whether the 'countersign' and 'confirm password' matches. If both the password matches, and so the code further runs.
Line 29 – 32: Checking whether the countersign matches or non.
Line 35 – 46: If the number of errors until this point is zero, the password is then 'md5' encrypted and the data entered is sent to the database. After the registration process is complete, the username is stored in the session variable, and the user is redirected to the index.php folio, where he is asked to enter the login credentials.
Line 50 – fourscore: Kickoff the username and password entered in sanitized. This is essential to increase database security, as it eliminates the chances of any SQL injection. The user gets an error message if the username or the password field is left bare.
If the number of errors until this betoken of lawmaking is constitute to exist 0, then a database check is run. If the username entered past the user is establish to be nowadays in the database, then the user successfully logs in. The user is and so redirected to the 'index.php' page.
login.php

html

<? php include('server.php') ?>

<!DOCTYPE html>

< html >

< head >

< title >

Login and Registration

Organisation - LAMP Stack

</ title >

< link rel = "stylesheet" type = "text/css"

href = "way.css" >

</ head >

< body >

< div class = "header" >

< h2 >Login Here!</ h2 >

</ div >

< class method = "post" action = "login.php" >

<? php include('errors.php'); ?>

< div class = "input-group" >

< label >Enter Username</ label >

< input type = "text" name = "username" >

</ div >

< div class = "input-group" >

< characterization >Enter Countersign</ label >

< input type = "password" proper noun = "password" >

</ div >

< div class = "input-group" >

< push button type = "submit" class = "btn"

proper name = "login_user" >

Login

</ button >

</ div >

< p >

New Here?

< a href = "register.php" >

Click here to register!

</ a >

</ p >

</ form >

</ body >

</ html >

Explanation: Login folio of the system. The user has to enter the username and countersign to successfully log in. After the login button is pressed, the login lawmaking written in the server.php page is run, which does all the backend work, similar checking whether the username and countersign match or non.
annals.php

php

<?php include ( 'server.php' ) ?>

<!DOCTYPE html>

<html>

<head>

<title>

Registration organisation PHP and MySQL

</title>

<link rel= "stylesheet" blazon= "text/css"

href= "way.css" >

</head>

<body>

<div class = "header" >

<h2>Register</h2>

</div>

<class method= "post" action= "register.php" >

<?php include ( 'errors.php' ); ?>

<div class = "input-group" >

<label>Enter Username</label>

<input blazon= "text" proper name= "username"

value= "<?php echo $username; ?>" >

</div>

<div course = "input-group" >

<characterization>Email</characterization>

<input blazon= "email" name= "email"

value= "<?php echo $e-mail; ?>" >

</div>

<div class = "input-group" >

<label>Enter Password</label>

<input type= "password" name= "password_1" >

</div>

<div course = "input-grouping" >

<label>Confirm password</characterization>

<input type= "password" name= "password_2" >

</div>

<div class = "input-group" >

<push button type= "submit" class = "btn"

name= "reg_user" >

Register

</button>

</div>

<p>

Already having an account?

<a href= "login.php" >

Login Here!

</a>

</p>

</form>

</trunk>

</html>

Explanation: This folio contains the HTML coding of the registration page. The 'server.php', and 'errors.php' pages are included in lines 01 and fifteen respectively. This is necessary to brand the backend of the registration organization work. The user is asked to enter the username, email, and countersign to create an account. After the input fields are filled, the data entered is sent to the database tabular array.
alphabetize.php

html

<? php

// Starting the session, to utilize and

// shop data in session variable

session_start();

// If the session variable is empty, this

// ways the user is withal to login

// User will be sent to 'login.php' page

// to let the user to login

if (!isset($_SESSION['username'])) {

$_SESSION['msg'] = "Yous have to log in first";

header('location: login.php');

}

// Logout button will destroy the session, and

// will unset the session variables

// User will be headed to 'login.php'

// after logging out

if (isset($_GET['logout'])) {

session_destroy();

unset($_SESSION['username']);

header("location: login.php");

}

?>

<!DOCTYPE html>

< html >

< head >

< title >Homepage</ title >

< link rel = "stylesheet" type = "text/css"

href = "way.css" >

</ head >

< body >

< div grade = "header" >

< h2 >Habitation Folio</ h2 >

</ div >

< div grade = "content" >

<? php if (isset($_SESSION['success'])) : ?>

< div grade = "error success" >

< h3 >

<? php

echo $_SESSION['success'];

unset($_SESSION['success']);

?>

</ h3 >

</ div >

<? php endif ?>

<? php if (isset($_SESSION['username'])) : ?>

< p >

Welcome

< strong >

<? php echo $_SESSION['username']; ?>

</ strong >

</ p >

< p >

< a href = "index.php?logout='1'" way = "color: red;" >

Click here to Logout

</ a >

</ p >

<? php endif ?>

</ div >

</ body >

</ html >

Explanation:
Line 01 – 19: The username that was stored in the session variable is now displayed back to the user. This session variable can either be destroyed using unset($_SESSION["products"]) or session_destroy(). Notwithstanding, session_destroy() will destroy all the session variables at once. To destroy only the 'username' session variable, it will be improve to unset the variable using unset($_SESSION["products"]).
Line 34 – 42: This makes sure that this page is accessible only to those users that are logged in.
Line 45 – 50: This displays a personalized welcome message to the user once they log in.
CSS File

CSS

* {

margin : 0px ;

padding : 0px ;

}

trunk {

font-size : 120% ;

background : #F8F8FF ;

}

.header {

width : 30% ;

margin : 50px motorcar 0px ;

colour : white ;

background : #5F9EA0 ;

text-marshal : eye ;

border : 1px solid #B0C4DE ;

edge-bottom : none ;

border-radius: 10px 10px 0px 0px ;

padding : 20px ;

}

form, .content {

width : thirty% ;

margin : 0px machine ;

padding : 20px ;

edge : 1px solid #B0C4DE ;

background : white ;

edge-radius: 0px 0px 10px 10px ;

}

.input-group {

margin : 10px 10px 10px 10px ;

}

.input-grouping label {

display : block ;

text-align : left ;

margin : 5px ;

font-size : 20px ;

}

.input-group input {

height : 32px ;

width : 95% ;

padding : 5px 10px ;

font-size : 15px ;

edge-radius: 10px ;

edge : 1px solid gray ;

}

.btn {

cursor : pointer ;

padding : 12px ;

font-size : 16px ;

colour : white ;

background : #23585a ;

border : none ;

edge-radius: 10px ;

}

.fault {

width : 92% ;

margin : 0px auto ;

padding : 10px ;

edge : 1px solid #a94442 ;

colour : #a94442 ;

background : #f2dede ;

border-radius: 5px ;

text-align : left ;

}

.success {

color : #3c763d ;

background : #dff0d8 ;

border : 1px solid #3c763d ;

margin-lesser : 20px ;

}

Pictorial Representation:

Registration Page

User is redirected to this page after logging in. A welcome bulletin is displayed there.

Login page of the system

User has successfully logged in

Incorrect username and password combination

How to run this project?

The source codes of this projection can be obtained from this GitHub repository.
After downloading and unzipping the project, follow the given steps to run the plan:

  • Download all the files, or clone the repository into your local organization.
  • Create a database named 'registration', and a tabular array named 'users'. The MySQL code of the table has been provided in a higher place.
  • Utilize XAMP or WAMP to run the arrangement on localhost.
  • Make sure that the necessary ports to run Apache and MySQL server are free. If not, then y'all will have to change the port numbers.

HTML is the foundation of webpages, is used for webpage evolution by structuring websites and web apps.You lot tin learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

PHP is a server-side scripting language designed specifically for spider web evolution. Yous can learn PHP from the ground up by post-obit this PHP Tutorial and PHP Examples.


danielscombehe.blogspot.com

Source: https://www.geeksforgeeks.org/how-to-display-logged-in-user-information-in-php/

Postar um comentário for "Show Separate Uploads as Individual Lists Php Mysql"