So something like this:
<?php
session_start();
include 'dbconnection.php'; // include your database file that connects and selects teh database to use.
if(isset($_POST['submit']) && $_POST['user'] != "" && $_POST['pass'] != "")
{
$username = htmlentities(trim($_POST['user']), ENT_QUOTES); // Puts any HTML entities into their ascii value ie: < become < and also put any quootes in to ascii value too. Search for these functions at php.net. Also the trim just removes any spaces that is at the start and end of the string.
$password = md5($_POST['pass']);
$users = "SELECT username, password FROM usrrs WHERE username='$username' AND password='$password' AND admin='Y'";
$get_users = mysql_query($users, $con); // $con is the variable for your database connection.
if(mysql_num_rows($get_users) == 1)
{
$_SESSION['username'] = $username;
$_SESSION['logged_in'] = 1;
header("Location: admin.php"); // change admin.php to the actuall page you want the user to be sent to.
}
else
{
$error = "<span style=\"color: red\";>Login information invalid!</span><br/>";
}
}
?>
Okay, so the above script checks wheather the buttton named submit has been passed and checks wheather the username and password arn't blank, else it'll activate the error.
Now it'll put the $_POST variables in to suitable variables to deal with. Next we prepare the MySQL query ready. The query is straight forward, It selects the username and password fields form the users table and checks whether the username and password submited matches aswell as the user is an Admin, which the value has to be Y.
Next we create another variable for the actully MySQL query to get the users. If there is a match with the username and password and is equal to 1 one, meaning one positive match. So if we do have one positive matych then we'll set up our sessions, which is username and is_logged. AN then send the user the admin section.
Now if the username isn't equal to one, either MySQL found now results or more results it'll through the error message.
I hope you got that.
Now the form:
<form actiom="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<div style="padding:5px 0px 10px 0px;">
<?php if(isset($error)) echo $error; ?> <!-- Checks wether the variable $error has been set, otherwise it wont display. -->
Please login to access the AdminCP</div>
</tr>
<tr style="height:25px">
<td width="33%" align="right" valign="middle">Username: </td>
<td width="67%" align="left">
<input name="user" type="text" size="25" maxlength="30" />
</td>
</tr>
<tr style="height:25px">
<td align="right" valign="middle">Password: </td>
<td align="left">
<input name="pass" type="password" size="25" maxlength="30" />
</td>
</tr>
<tr style="height:25px" align="left">
<td> </td>
<td>
<input type="submit" name="submit" value="Login" class="submit" />
</td>
</tr>
</table>
</form>
The form code is pretty starte forward and self explanitary. Remeber the PHP script has checks whether the submit button has been submitteed, notice it has a name as submit and a value is what is set for the login precess to be triggered.
NOTE: The above script is for an actuall project I am doing now. SO you're lucky! You may have to change the MySQL query to suit your needs, aswell as the form.
Also yes this script is for logging into an Administration Page.
Also on every page you want protected apply this code:
<?php
session_start();
if(!isset($_SESSION['logged_in']) || isset($_SESSION['logged_in']) != 1)
{
session_destroy();
header("Location: ./index.php");
exit;
}
if(isset($_GET['logout']) == 1)
{
session_destroy();
header("Location: ./index.php");
}
?>
Basically this checks whether the session logged_in is still set and is set to one else it'll destroy the session and send them back to the index page which has the lolgin form on the page. Also this code:
if(isset($_GET['logout']) == 1)
{
session_destroy();
header("Location: ./index.php");
}
Chekcs whether the variable logged out is true, which do by using this HTML code:
<a href="?logout=1">Logout</a>
Okay I hope you understand what this script does and how to check if values are set etc.
Also if ou don't knwo what some of the functions in the code do hop over to php.net to check them out.
Edited by softLearner, 27 July 2005 - 04:51 AM.