Jump to content

Archived

This topic is now archived and is closed to further replies.

willtech

user login different page! Help!

Recommended Posts

ok i have a php code taht lets me have users and they login but i need each of them to login to there own page not the same page help!

this is what i got and i cant make it do idividual pages.

Have you ever wanted to make a login script so you can have membercontent?
This tutorial will show you how using php. No database will be used.

1) Creating a form
Here is the code for making the form. The form asks for your username and password. Save this code as login.php


Code:
<html>
<head>
<title>Please login</title>
</head>
<body>
<?php
if(isset($_GET["wrong"])){
echo("<b>Username or password is incorrect!<br />Please try again.</b>");
}
?>

<form action="login2.php" method="post">
<br />
Username:<br />
<input type="text" name="username" /><br />
Password:<br />
<input type="password" name="password" />
<br />
<br />
<input type="submit" value="Login" />
</form>

</body>
</html>
The piece of php code will tell the viewer that he or she typed a wrong username or password.
between the <form></form> tags we have 3 input fields. 1 for the username, 1 for the password, and 1 that will submit the inputs and send it to login2.php.
Note that the form will be sent using the "post" method. This is the safest.

2)Checking the userinput
In this step we will check if the username and password are correct.
This is done with php. No html will be used.
Create a new page and call it login2.php. Give it the following content:

PHP Code:
<?php
$usernames = array("user1", "user2", "user3", "superman");
$passwords = array("pass1", "pass2", "password3", "supermans password");
$page = "mypage.php";



for($i=0;$i<count($usernames);$i++){
  $logindata[$usernames[$i]]=$passwords[$i];
}
if($logindata[$_POST["username"]]==$_POST["password"]){
session_start();
$_SESSION["username"]=$_POST["username"];
header('Location: '.$page);
exit;
}else{
header('Location: login.php?wrong=1');
exit;
}
?>
You might want to edit the first 3 lines.
1)$usernames is an array with all the usernames. To add a new one:

PHP Code:
$usernames = array("user1", "user2", "user3", "superman", "new one"); 

2)$passwords is an array with all the passwords. To add a new one:

PHP Code:
$passwords = array("pass1", "pass2", "password3", "supermans password", "new one"); 

Make sure that the username and the password have the same position.
user1 will be able to login with the password pass1, not with any other password. user2 with pass2 and no other.
3)$page is the page the user will go to when he is logged in. Change mypage.php to the page you would like to be the main page for logged in users. make sure the quotationmarks are still around it!

3) Checking if the user is still logged in
Create a new page, call it login3.php with the following code:

PHP Code:
<?php
session_start();
if(!isset($_SESSION["username"]){
header('Location: login.php');
exit;
}
?>

This code simply checks if the sesion that we made in login2.php has a value.
If it doesn't it sends you to the login page.

4) Making your pages protected
All the pages that you want to be protected need to have a .php extension! otherwise they cannot be protected. Add this piece of code to the first line.

PHP Code:
<?php require("login3.php"); ?>
Make sure it is on the first line. Otherwise it will give you an error.
Now you pages are protected! simple?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.