Jump to content

Simple PHP Login Problem


mccrum

Recommended Posts

I made a simple PHP login form that uses only 1 username and password to login. If I currently type in the correct username and password it displays "LOGIN SUCCESSFUL" and shows a link to the admin page but I would like to include the admin page within the php file. Below is the code I have used, hope you can help.

 

 

<!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" dir="ltr" lang="en-US">

 

<head>

<title>Energy Saving › Admin Log In</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel='stylesheet' href='login.css' type='text/css' media='all' />

</head>

 

<div class="container">

<div id="header"></div>

 

<body class="login">

<div id="login">

 

<h1><a href="login.html">Energy Saving Logo</a></h1>

 

<?php

 

$loginname = "admin";

$loginpassword = "password";

 

if ($_POST['text'] != $loginname || $_POST['password'] != $loginpassword) {

 

?>

 

<div id="login_error"> <strong>ERROR</strong>: Incorrect Username or Password.<br /></div>

 

<?php

 

}

else {

 

?>

 

<div id="login_sucess"> <strong>LOGIN SUCCESSFUL</strong>: <a href="login.html">Click here</a> to continue.</div>

 

<?php

 

}

 

?>

 

 

 

<form name="login" id="loginform" action="login.php" method="post">

<p>

<label>Username:<br />

<input type="text" name="text" id="user_login" class="input" value="" size="20" tabindex="10" /></label>

</p>

<p>

<label>Password:<br />

<input type="password" name="password" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>

</p>

<p class="submit">

<input type="submit" name="submit" id="submit" value="Log In" tabindex="100" />

</p>

</form>

</div>

 

</body>

</html>

Link to comment
Share on other sites

<!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" dir="ltr" lang="en-US">

<head>
<title>Energy Saving › Admin Log In</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel='stylesheet' href='login.css' type='text/css' media='all' />
</head>

<div class="container">
<div id="header"></div>

<body class="login">
<div id="login">

<h1><a href="login.html">Energy Saving Logo</a></h1>

<?php

$loginname = "admin";
$loginpassword = "password";

if ($_POST['text'] != $loginname || $_POST['password'] != $loginpassword) {

?>

<div id="login_error"> <strong>ERROR</strong>: Incorrect Username or Password.<br /></div>

<?php

}
else {

?>

<div id="login_sucess"> <strong>LOGIN SUCCESSFUL</strong><!-- : <a href="login.html">Click here</a> to continue.-->
    <?php include("login.html"); ?>
</div>

<?php

}

?>



<form name="login" id="loginform" action="login.php" method="post">
<p>
<label>Username:<br />
<input type="text" name="text" id="user_login" class="input" value="" size="20" tabindex="10" /></label>
</p>
<p>
<label>Password:<br />
<input type="password" name="password" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
</p>
<p class="submit">
<input type="submit" name="submit" id="submit" value="Log In" tabindex="100" />
</p>
</form>
</div>

</body>
</html> 

Link to comment
Share on other sites

Ok I think I have it working now, the only problem is I can not get the error to show up if you enter the wrong username or password. I would like the same error box to show up as I had in the first set of code I posted, here is my new code without the error, currently it just reloads the login form if you enter a wrong username/password:

 

 

<!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" dir="ltr" lang="en-US">

 

<head>

<title>Energy Saving › Admin Log In</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel='stylesheet' href='login.css' type='text/css' media='all' />

</head>

 

<div class="container">

<div id="header"></div>

 

<body class="login">

<div id="login">

 

<h1><a href="login.html">Energy Saving Logo</a></h1>

 

 

<?php

 

 

 

// Define your username and password

 

$username = "admin";

 

$password = "password";

 

 

 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

 

 

 

?>

 

 

<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 

<label>Username:<br />

<input type="text" name="txtUsername" id="user_login" class="input" value="" size="20" tabindex="10" /></label>

</p>

<p>

<label>Password:<br />

<input type="password" name="txtPassword" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>

</p>

<p class="submit">

<input type="submit" name="submit" id="submit" value="Login" tabindex="100" />

</p>

</form>

</div>

 

 

 

<?php

 

 

 

}

 

else {

 

 

 

?>

 

 

 

<p>This is the protected page. Your private content goes here.</p>

 

 

 

<?php

 

 

 

}

 

 

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.