Current location: Hot Scripts Forums » Programming Languages » PHP » Simple PHP login system


Simple PHP login system

Reply
  #1 (permalink)  
Old 01-26-06, 08:24 AM
dennell dennell is offline
Newbie Coder
 
Join Date: Dec 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Simple PHP login system

Hi there i was just wondering whether someone could help me with a little php.

I want to create a small yet simple PHP based login system, where by when a user logs in depeding on their username and password will be directed to a different webpage.
So if the username and password is "jimmy" and "password" they will then be directed to jimmy.php

or
If the username was "sarah" and "password" then she would be redirected to sarah.php.

This system will not be a large system, and will only have like 4 users so i dont think a backend mysql database will be needed for the users!

Is this possible
Reply With Quote
  #2 (permalink)  
Old 01-26-06, 09:13 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I was bored and wrote one quick.

Save this as "login.php". You can add as much users as you want. Just use a higher number than the previous between the "[" and "]".

PHP Code:

<?php

session_start
();

if (isset(
$_SESSION["user"])) {

    
header("Location: "$_SESSION["user"] .".php");
}


$username[1] = "nico";
$password[1] = "nico";

$username[2] = "sarah";
$password[2] = "sarah";

$username[3] = "jimmy";
$password[3] = "jimmy";

$post_user $_POST["user"];
$post_pass $_POST["pass"];


if (isset(
$_POST["Submit"])) {
    
    
$nr 1;

    while (isset(
$username[$nr])) {
    
        if ((
$username[$nr] == $post_user) && ($password[$nr] == $post_pass)) {
            
                if (
file_exists($username[$nr]. ".php")) {
                
                    
$_SESSION["user"] = $username[$nr];
                    
                    
header("Location: "$username[$nr] .".php");
                    
                } else {
                
                    die(
"Cannot open userpage");
                    
                }
            
        }
        
         
$nr++;
         
    }
    
}
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log in</title>

</head>

<body>
<?php
if (isset($_GET["logout"])) {

    echo 
"Successfully logged out!";
    
}
?>
<form action="" method="post" name="login" id="login">
  <p>
    <input name="user" type="text" id="user"> 
  username</p>
  <p>
    <input name="pass" type="password" id="pass"> 
    password</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
</body>
</html>
And this as "logout.php"

PHP Code:

<?php

session_start
();

if (!
$_SESSION["user"]) {
    
    
header("Location: login.php");
    
} else {

    unset(
$_SESSION["user"]);
    
    
session_destroy();
    
    
header("Location: login.php?logout=1");
    
}

?>

And this as userpage. "sarah.php" for example. Or whatever you used as username
PHP Code:

<?php

session_start
();

if (!
$_SESSION["user"]) {

    
header("Location: login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>User page</title>

<script type="text/javascript">

function LogOut() {

    var answer = confirm("Are you sure you want to log out?");
    
        if (answer != 0) {
        
            window.location = "logout.php";
        
        }
        
}

</script>


</head>

<body>
<p>You are logged in as &quot;<?php echo $_SESSION["user"?>&quot;</p>
<p><a href="javascript:LogOut();">Click here to log out </a></p>
</body>
</html>
Hope this helps!

Oh, one last thing. Make sure all files are in the same directory!
Reply With Quote
  #3 (permalink)  
Old 02-26-06, 12:54 AM
umesh umesh is offline
New Member
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
thanx

i thank u both for posting as well giving the perfect solution
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i need a simple login and registration php script plz lunarboi Script Requests 3 01-03-06 12:04 AM
Simple PHP Login/Member System Lycon Script Requests 1 02-18-05 10:30 PM
Looking for a decent PHP script (login system) to handle clients and orders, ANY1? xero PHP 0 08-29-04 06:22 PM
question about simple login system magsec4 PHP 5 02-23-04 03:35 AM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM


All times are GMT -5. The time now is 06:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.