• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Login script

Reg Web

New Member
I have just made a login script, but how can I allow for multiple usernames/passwords? All I got at the moment is:

<form>
<p>USER NAME :
<input type="text" name="text2">
</p>
<p>PASSWORD :
<input type="password" name="text1">
<input type="button" value="Login" name="Submit" onclick=javascript:validate(text2.value,"1039atc",text1.value,"*****") >
</p>

</form>
<script language = "javascript">

/*
Script by Anubhav Misra (anubhav_misra@hotmail.com)
Submitted to JavaScript Kit (http://javascriptkit.com)
For this and 400+ free scripts, visit http://javascriptkit.com
*/

function validate(text1,text2,text3,text4)
{
if (text1==text2 && text3==text4)
load('http://members.1039online.org/frontpage.php');
else
{
load('http://members.1039online.org/failedlogin.php');
}
}
function load(url)
{
location.href=url;
}
</script>
 
Please, please don't actually use this code for a login system...please.

Use some sort of server side application, or .htaccess. Not JavaScript, which someone can easily view the code in plain text.
 
Its really easy to make a login script with php.
PHP:
<?php
if($action=="login") {
$user = $_POST['user'];
$pass = $_POST['pass'];
if(($user == "user1" && $pass == "pass1") || ($user == "user2" && $pass == "pass2")) {
header(Location:frontpage.php);
}

else
{
header(Location:failed.php);
}
}

if(!$action) {
?>
<form action="?action=login" method="post">
<p>USER NAME :
<input type="text" name="user">
</p>
<p>PASSWORD :
<input type="password" name="pass">
<input type="button" value="Login" name="Submit">
</p>
</form>
<?php
}
?>
 
Last edited:
Use sessions or cookies. Sessions destroyed when the browser is closed and cookies are stored on the computer. But some people may disable cookies. So I recommend you use both.

PHP:
<?php 
if($action=="login") { 
$user = $_POST['user']; 
$pass = $_POST['pass']; 
if(($user == "user1" && $pass == "pass1") || ($user == "user2" && $pass == "pass2")) { 
$pw = md5($pass); // Encrypt the password
setcookie ("auth",$user-$pw,time()+1957240,"/"); // Create the cookie, storing the  username and encrypted pass.
session_register("auth"); // set a session in case cookies are disabled
header(Location:frontpage.php); 
} 

else 
{ 
header(Location:failed.php); 
} 
} 

if(!$action) { 
?> 
<form action="?action=login" method="post"> 
<p>USER NAME : 
<input type="text" name="user"> 
</p> 
<p>PASSWORD : 
<input type="password" name="pass"> 
<input type="button" value="Login" name="Submit"> 
</p> 
</form> 
<?php 
} 
?>

Now to make sure the cookie or session is set, on frontpage.php or the pages you want to be protected put this on the top, very top or you'll get header errors.
PHP:
<?php
$cookie = $_COOKIE['auth'];
session_start(); // You need this to read session variables

if (empty($cookie) || !isset($cookie)) // if the cookie is empty or not set
{
// Read the session
if (empty($_SESSION['auth']) || !isset($_SESSION['auth'])) // the session is empty or not set
{
header("Location: login.php"); // Redirect to login.php
exit; // Stop any further coding
}
?>
 
Rolly: your script is fine, but it is in ASP, so I cannot use it!

R4g1ng: Cannot test your script as nothing happens when I click "login"!

Can anyone please help me with this?
 
PHP:
<?php
if($action=="login") {
$user = $_POST['user'];
$pass = $_POST['pass'];
if(($user == "user1" && $pass == "pass1") || ($user == "user2" && $pass == "pass2")) {
$pw = md5($pass); // Encrypt the password
setcookie ("auth",$user-$pw,time()+1957240,"/"); // Create the cookie, storing the  username and encrypted pass.
session_register("auth"); // set a session in case cookies are disabled
header(Location:frontpage.php);
}

else
{
header(Location:failed.php);
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>?action=login" method="post">
<p>USER NAME :
<input type="text" name="user">
</p>
<p>PASSWORD :
<input type="password" name="pass">
<input type="submit" value="Login" name="submit">
</p>
</form>
 
Last edited:
Hi R4Ging.. Nice tutorial :) I learn more now :)

Just wonder let say that I will have a login form. Then after user login then the login form will change to
Hello $_POST['user'],
Then there are two links cpanel and billing.
Is there any way that with those login information the user can directly login to cpanel page and billing?

In this case I think it will need to integrate with clientexec database maybe.

Sorry a bit not clear english :)

Anyway. Is there any possibility one login, that will access to two logins (cPanel and billing (cilentexec))? Let say this case user & password is same.
I think it will be hard if the user and password different.

Thanks

Dan^^
 
I'm not sure because I don't know anything about the coding in those scripts. You might need someone more experienced to answer that question.
 
Back
Top