This time with my full code.... :s
Are you ready for this... 😐
I have passed this through an online PHP Syntax Checker - no errors. No guarantee it works either 😃 Which it doesn't 🙁
My appologies to both of you, I didn't use the switch:case... because I was still confused after trying them both.
Xager, you had brackets and numbered cases: switch($user_level){ case 0:
Halojoy, you had no brackets and tagged cases: switch( $usergroup ) : case 'admin' :
Being new to PHP, I have no idea which one is technically correct, or if both of them are, or how to relate them to my current code... yep - n00b !
My deciding factor on this page redirection is a variable called 'status', so where to put that into your snipets had me stumped.
However... I have an ALMOST working model from what I had already coded and your help on what the $info variable held for me.... yes, I learned that from your examples... thank you.
The only thing it does wrong now is sends EVERYONE to the first 'if' clause - the Admin page. I've shuffled them around - and whichever 'if' I put first - everyone goes. So my code is not 'checking' the 'status'. It KNOWS the status, because I threw an - echo "Your status is $_STATUS<p>"; on the target page, it shows the correct status for each person logged in... including inactive - who SHOULDN'T be able to login - DOH! 😕
If a live test would help you see what is happening, I can arrange to have this page on a server for you to look at. Thank you.
So here's my code. I hope it's not just one big balls-up, which has to be re-written :s
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>AllStars - Login</title>
<script type="text/javascript">
window.onload=function() {
document.forms[0][0].focus();
}
</script>
</head>
<body>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"><font face="Tahoma" size="6">
<a href="index.php">
<img border="0" src="star_logo.gif" width="284" height="292"></a></font></p>
<p align="center"><font face="Tahoma" size="6">Please Login</font></p>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0" width="100%" height="74">
<tr>
<td width="38%" height="22"> </td>
<td width="6%" height="22">Username:</td>
<td width="14%" height="22"><input type="text" name="username" maxlength="40" size="20"></td>
<td width="53%" height="22"> </td>
</tr>
<tr>
<td width="38%" height="22"> </td>
<td width="6%" height="22">Password:</td>
<td width="14%" height="22"><input type="password" name="pass" maxlength="50" size="20"></td>
<td width="53%" height="22"> </td>
</tr>
<tr>
<td width="38%" height="26"> </td>
<td width="6%" height="26"> </td>
<td width="14%" height="26">
<p align="center">
<input type="submit" name="submit" value="Login" style="float: right"></td>
<td width="53%" height="26"> </td>
</tr>
</table>
</form>
</body>
</html>
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// Connects to your Database
mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("this_DB") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $COOKIE['ID_my_site'];
$pass = $COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: main.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit']))
{ //if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass'])
{
header("Location: failed.php");
exit;
}
// checks it against the database
if (!get_magic_quotes_gpc())
{
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0)
{
header("Location: failed.php");
exit;
}
else
{
while($info = mysql_fetch_array( $check ))
{
$_STATUS = $info['status'];
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password'])
{
header("Location: failed.php");
exit;
}
else
if($_STATUS = 'admin')
{
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
header("Location: admin.php");
}
elseif($_STATUS = 'active')
{
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
header("Location: main.php");
}
elseif($_STATUS = 'inactive')
{
echo "inactive.php";
exit;
}
}
}
}
?>