Hello, I've created this topic for individuals like myself who are trying to understand how to build a single login page but then have it show customized information/ results for different users.
For example, user 1 logs in to check his product shipment/order status and sees that his product is still in the warehouse.
Whereas user 2 may just see a confirmation that his product has shipped.
Thanks.
Login Page w/ Customized Result
A single Login Page that shows a different results page depending on t
Page 1 of 1
Login Page w/ Customized Result A single Login Page that shows a different results page depending on t
#2
Posted 04 December 2008 - 02:29 AM
Okay heres a very simple login system.
- It doesnt use a database to store any of the details
- It doesnt use a hashed (encrypted) password
- Username and password are hardcoded
- Only able to deal with a small number of known users
- Uses simple validation
But if your new to PHP it is hopefully helpful to you.
Okay first things first. Create 2 blank php files. Name one of them named admin.php the other can be named login.php or whatever you wish. In my example im calling mine index.php
In the index.php lets enter our html form:
Lets go through it.
You will notice we start again with the session_start(); function. This allows us to access sessions which have been set on the server (such as the one we created earlier in our log in)
We then check if any session named username has been set (they may have come to this page accidentally, we only want people from the log in coming here)
If one has been set, meaning either John or Mark we test their session and display the relevant data:
If one hasn't been set we immediately re-direct them to the login.
And thats it! You can find a working example here: http://www.sypher-design.co.uk/tutorials/s...login/index.php
Any questions or problems let me know!
- It doesnt use a database to store any of the details
- It doesnt use a hashed (encrypted) password
- Username and password are hardcoded
- Only able to deal with a small number of known users
- Uses simple validation
But if your new to PHP it is hopefully helpful to you.
Okay first things first. Create 2 blank php files. Name one of them named admin.php the other can be named login.php or whatever you wish. In my example im calling mine index.php
In the index.php lets enter our html form:
<?php
//Check if the user has tried to login
if (isset($_POST['submit'])) { //Checks to see if the user has submitted the form
$username = $_POST['username']; //Set username to a variable
$password = $_POST['password']; //Set password to a variable
$error = FALSE; //Setting the variable for further use, no error yet!
if (!$username | !$password) { //Simple validation check to see if they entered data into both fields
$error = TRUE; //Theres an error!
$msg .= "Please enter both a <strong>username</strong> and <strong>password</strong>!<br />";
}
if ($username == "John" && $password == "website") {
// Username and password are right! Forward them to the admin panel
session_start();
$_SESSION['username'] = $username; //Start a session to determine it is John
echo "<p>Thank you! now forwarding you to the admin panel</p>";
echo "<meta http-equiv=\"refresh\" content=\"4;url=http://www.yourwebsite.com/admin.php\">";
// Redirect user after 4 seconds to admin page
}
else if ($username == "Mark" && $password == "design") {
// Username and password are right! Forward them to the admin panel
session_start();
$_SESSION['username'] = $username; //Start a session to determine it is Mark this time
echo "<p>Thank you! now forwarding you to the admin panel</p>";
echo "<meta http-equiv=\"refresh\" content=\"4;url=http://www.yourwebsite.com/admin.php\">";
// Redirect user after 4 seconds to admin page
}
else {
$error = TRUE; //There was an error!
$msg .= "Username and/or Password are incorrect! Please try again.<br />";
}
}
?>
Lets go through it.
<?php
session_start(); //Have to set this when you want to edit or access sessions
if (isset($_SESSION['username'])) { //Check to see if session exists, if so display information!
$username = $_SESSION['username']; //Set the session to a variable
if ($username == "John") { //If its John who has logged in
echo "<p>Welcome <strong>John!</strong><br /> Here is all your data!</p>";
}
else if ($username == "Mark") { //If its Mark who has logged in
echo "<p>Welcome <strong>Mark!</strong><br /> Here is all your data!</p>";
}
}
else { //Redirect user to login page if they came here by accident!
echo "Oops wrong page!";
echo $_SESSION['username'];
header('Location: [url="http://www.sypher-design.co.uk/tutorials/simplephplogin/index.php'%3b%29;"]http://www.sypher-design.co.uk/tutorials/s...ndex.php');[/url]
}
?>
You will notice we start again with the session_start(); function. This allows us to access sessions which have been set on the server (such as the one we created earlier in our log in)
We then check if any session named username has been set (they may have come to this page accidentally, we only want people from the log in coming here)
if (isset($_SESSION['username'])) {
If one has been set, meaning either John or Mark we test their session and display the relevant data:
if ($username == "John") { //If its John who has logged in
echo "<p>Welcome <strong>John!</strong><br /> Here is all your data!</p>";
}
If one hasn't been set we immediately re-direct them to the login.
else { //Redirect user to login page if they came here by accident!
echo "Oops wrong page!";
echo $_SESSION['username'];
header('Location: http://www.sypher-design.co.uk/tutorials/simplephplogin/index.php');
}
And thats it! You can find a working example here: http://www.sypher-design.co.uk/tutorials/s...login/index.php
Any questions or problems let me know!
#3
Posted 04 December 2008 - 07:55 PM
hi sypher, thank you for putting this together. i'll take a crack at this over the weekend and hopefully can get it working.
#4
Posted 07 December 2008 - 09:26 PM
hi sypher,sorry for the late reply but i wanted to let you know it worked out.
i'm going to spend some time trying to figure out how to do a database, i.e. someone registers there own username and password but will post a note if i'm having trouble doing this.
thanks again.
i'm going to spend some time trying to figure out how to do a database, i.e. someone registers there own username and password but will post a note if i'm having trouble doing this.
thanks again.
#5
Posted 07 December 2008 - 11:57 PM
Ah okay 
I'll probably write another tutorial down the line just for what you want to do.
I'll probably write another tutorial down the line just for what you want to do.
#6
Posted 08 December 2008 - 01:58 PM
sypher you are too good to be true. thanks again.
#7
Posted 13 December 2008 - 12:06 AM
hi sypher, you haven't happened to developed that new tutorial yet have you? thanks.
Share this topic:
Page 1 of 1


Help
MultiQuote









