Jump to content

Login Page w/ Customized Result

A single Login Page that shows a different results page depending on t

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Login Page w/ Customized Result A single Login Page that shows a different results page depending on t Rate Topic: -----

#1 User is offline   lcdsi 

  • W.R. Private
  • Group: Members
  • Posts: 9
  • Joined: 01-December 08

Posted 02 December 2008 - 11:51 PM

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.
0

#2 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

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:
<?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!
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#3 User is offline   lcdsi 

  • W.R. Private
  • Group: Members
  • Posts: 9
  • Joined: 01-December 08

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.
0

#4 User is offline   lcdsi 

  • W.R. Private
  • Group: Members
  • Posts: 9
  • Joined: 01-December 08

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.
0

#5 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

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.
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#6 User is offline   lcdsi 

  • W.R. Private
  • Group: Members
  • Posts: 9
  • Joined: 01-December 08

Posted 08 December 2008 - 01:58 PM

sypher you are too good to be true. thanks again.
0

#7 User is offline   lcdsi 

  • W.R. Private
  • Group: Members
  • Posts: 9
  • Joined: 01-December 08

Posted 13 December 2008 - 12:06 AM

hi sypher, you haven't happened to developed that new tutorial yet have you? thanks.
0

#8 User is offline   EncoderDecoder 

  • W.R. Sergeant
  • Group: Members
  • Posts: 260
  • Joined: 01-March 11
  • Gender:Female
  • Location:Philippines

Posted 31 May 2011 - 10:14 AM

wow, nice code..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users