Skip to content
Search for:
Xybernetics
  • About Us
  • Services
  • Solution
    • uCFlowCal
    • uCMobile HMI
    • Weather Station
    • Storage Management System (SMS)
    • Warehouse Management System (WMS)
    • Alarm Callout Sys
    • XynetAR : The Augmented Reality App
    • Back To School – The Bus Tracker
  • The Geek
  • Contact Us
  • App Privacy Policy
Tech Talk : PHP – Login Session Codes By Example
n1849778
by Rabi Gurung

Tech Talk : PHP - Login Session Codes By Example

January 8, 2018 No comment(s) PHP, Programming, TechTalk Example Script, Login, PHP, Sample Code, Session
Facebook
Twitter
Google+
LinkedIn
Pinterest

Tech Talk : PHP – Login Session Codes By ExampleAs I journey though my life as a professional web developer (and software developer), I encounter more and more of user login and session maintenance topics. It applies to all webpages these days be it in any platform; ASP.NET, JSP, PHP, PERL. There are tonnes of example, description, explanations and lecture notes out there, but the purpose of this note is not to get very academic but to give all you programmers a quick way to get start with a working example. So for the benefit of all new and experienced programmers alike, here are the steps in creating a login session based explanation and example codes.

Enjoy!

The Demo
Here is the link to the demo.
And here is are the codes in zip file.

The Codes

login.php : The “login.php” file the login screen

<?php
	// Begins the session, you need to say this at the top of a page or
	// before you call session code
	// Note that, you will need this statement all the time
	session_start();

	// Checking whether the session is already there or not if true
	// then header redirect it to the home page directly
	if(isset($_SESSION['use']))
	{
		header("Location:home.php");
	}

	if(isset($_POST['login']))   // Checks if user clicked login button
	{
		// Get value from encoded URL (POST)
		$user = $_POST['user'];
		$pass = $_POST['pass'];

		// Check for valid username and password (valid username
		// and password for this example is claire and iknoweverythng)
		if($user == "claire" && $pass == "iknoweverything")
		{
			// Store username in session variable
			$_SESSION['use']=$user;
			// On successful login redirects to home.php
			echo '<script type="text/javascript">';
			echo '	window.open("home.php","_self");';
			echo '</script>';
		}
		else
		{
		echo "Invalid username and/or password";
		}
	}
?>
<html>
	<head>
		<title>Login Page</title>
	</head>
	<body>
		<form action="" method="post">
		    <table width="200" border="0">
				<tr>
				<td>UserName</td>
				<td>
				<input type="text" name="user">
				</td>
				</tr>
				<tr>
				<td>Password</td>
				<td>
				<input type="password" name="pass">
				</td>
				</tr>
				<tr>
				<td colspan="2">
				<input type="submit" name="login" value="login">
				</td>
				</tr>
			</table>
		</form>
	</body>
</html>


home.php
: The “home.php” file is for homepage (the main landing page).

<?php
	// Begins the session, you need to say this at the top of a page or
	// before you call session code
	// Note that, you will need this statement all the time
	session_start();
?>
<html>
	<head>
		<title>Home</title>
	</head>
	<body>
<?php
	// If session is not set then redirect to login Page
	if(!isset($_SESSION['use']))
	{
		header("Location:Login.php");
	}
	echo $_SESSION['use'];
	echo "<br>";
	echo "Login Successful";
	echo "<a href='logout.php'> Logout</a> ";
?>
	</body>
</html>


logout.php
 : The “logout.php” file logs your user out of the session.

<?php
	session_start();
	echo "Logout Successfully";
	session_destroy();               // Destroys the session
	header("Location: login.php");
?>

 

n1849778
About the Rabi Gurung
I am a software developer in Calgary, Alberta, Canada with passion in getting my fingers in any new technology. I started software development when I was 10 years old and have never looked back. Now with well over 35 years of software development, I wanted to share some of these key knowledge with the people of the world in the wide web.
Categories
  • 3D Printing
  • 800xA
  • ABB
  • Acronis
  • Adobe After Effects
  • Allen Bradley
  • Android
  • Apache
  • App Store
  • Apple
  • Arduino
  • ASP.NET
  • Atmos
  • AVEVA
  • Azure DevOps
  • Barnett
  • Batch
  • Bedrock Automation
  • Beijer
  • blender
  • C
  • C#.Net
  • Catalina
  • CISCO
  • ClearSCADA
  • CompactLogix
  • Control Expert
  • ControlLogix
  • cURL
  • Debian
  • Docker
  • Eaton Xpert Meter 2000 PMX 2000
  • EIP3170
  • Electronics
  • Engineering
  • Excel
  • FactoryLink
  • FactoryTalkView
  • FAST/TOOLS
  • FASTTOOLS
  • GIMP
  • Github
  • Google
  • Google Chrome
  • Google Cloud Console
  • Google Drive
  • Google Fit
  • Google Map
  • Google Play
  • Harmony
  • Hitfilm
  • Horner
  • HTML
  • IIS
  • Infi90
  • iOS
  • iX Developer
  • JavaScript
  • Kepware
  • Kubernetes
  • LAMP
  • Lets Encrypt
  • Linux
  • Logic Designer
  • macOS
  • Matrikon
  • MB3170
  • Microhard
  • MicroLogix
  • Microsoft
  • Miscellaneous
  • MOXA
  • Mozilla Firefox
  • MySQL
  • Networking
  • NodeJS
  • Notepad++
  • OAuth2
  • OBS
  • Office 365
  • OSX
  • Outlook
  • PanelView
  • Particle
  • PHP
  • PIP
  • Pipeline
  • PlantPax
  • PLC-5
  • Postman
  • Powerpoint
  • Programming
  • Prosoft
  • ProTalk Link
  • Python
  • Rancher
  • Raspberry Pi
  • RDP
  • RealFlo
  • Redlion
  • Redlion
  • Redmine
  • RegEx
  • Remote Connection
  • Repo
  • Resource Configurator
  • Router
  • Samsung
  • SCADAPack
  • Schneider Electric
  • SCP
  • Selenium
  • SLC500
  • SQL
  • ssh
  • STARDOM
  • Stratix
  • Studio 5000
  • Sublime
  • TeamViewer
  • TechTalk
  • Telepace
  • TightVNC
  • Tindercad
  • Twitter
  • Ubuntu
  • Uncategorized
  • Unity Pro
  • Unity3d
  • VBA
  • VBA
  • Visual Studio
  • VMWare
  • vSphere
  • Well Test
  • Windows
  • WinSCP
  • Wonderware
  • Word
  • Xamarin
  • XCode
  • XMAL
  • Yokogawa
  • YouTube
  • Zip
Latest Posts
  • Enable Project Navigator and Project Toolbox In CScape For Horner PLC September 6, 2023
  • Setup multiple monitor display in Yokogawa FAST/TOOL September 6, 2023
  • Download And Upload Horner PLC Program September 2, 2023
Recent Comments
  • Betway Bonus Review - All You Need To Know on How to Move Windows 11 Icons to the Left Side of the Taskbar
  • Ron on How to Move Windows 11 Icons to the Left Side of the Taskbar
  • parimatch app on How to Move Windows 11 Icons to the Left Side of the Taskbar
Tags
800xA ABB Allen Bradley Android Apple Bedrock Automation Beijer C#.Net CompactLogix Control Expert ControlLogix Eaton Xpert Meter 2000 PMX 2000 Engineering Excel FactoryTalkView FASTTOOLS Harmony Infi90 MicroLogix Microsoft Miscellaneous MOXA PanelView Particle PLC-5 Programming Prosoft RealFlo Redlion Redlion SCADAPack Schneider Electric SLC500 STARDOM TechTalk Telepace Ubuntu Unity Pro VMWare Windows Wonderware Xamarin XCode XMAL Yokogawa
Calendar
September 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  
« Aug    
Xybernetics © 2023.
  • Facebook
  • Twitter
  • Youtube
  • Pinterest