Forum Moderators: coopster
<?php
// CHECK IF SESSION IS ALREADY SET
if($_SESSION["id"] == "1")
{
echo "<p>Welcome back ".$uname."! <a href='news.php'>News</a> |<a href='profile.php'>Profile</a> |<a href='logout.php'>Logout</a></p>";
}
// CHECK IF THE USER PRESSED SUBMIT TO ATTEMPT A LOGIN
if($_POST["userlogin"] == "submit") {
$username = $_POST["email"];
$password = substr(md5($_POST["password"]),0,16);
connectdb();
$query = mysql_query("SELECT name, email, password, account_type FROM ov_users WHERE email = '".$email."' AND password = '".$password."' AND account_type = '2'");
if(!$query) {
echo "<p>Oops, this is strange ... we cannot seem to log you in at the moment! Please try again in 5 minutes. If this problem occurs again, please contact our support department at <a href='mailto:support@example.com'>support@example.com</a></p>";
}
// This just assigns the users name to $uname so that I can use it as a message to welcome the user.
while($result = mysql_fetch_array($query)) {
$uname = $result["name"];
}
$num = mysql_num_rows($query);
// CHECK IF THE USER DID NOT SELECT REMEMBER ME, OBVIOUSLY CREATING A SESSION AS APPOSE TO A COOKIE.
if($num > 0) {
$_SESSION["username"] = $username;
$_SESSION["id"] = session_id();
$_SESSION["active"] = "1";
echo "<p>Welcome back ".$uname." Click <a href='profile.php'>here</a> to view your profile!</a></p>";
}
// CHECK IF THE USER DID SELECT REMEMBER ME. THIS CREATES A COOKIE CALLED cookie_id WITH A RANDOM STRING AND MD5. THIS THEN GETS SAVED IN THE DATABASE AND WILL BE RECALLED LATER.
if($num > 0 && $rememberme == "remember") {
setcookie("username",$username,time()+30754400);
$rand = rand(0,10000000);
set_cookie("cookie_id",$rand,time()+30754400);
$mdrand = md5($rand);
$query = mysql_query("UPDATE ov_users SET cookie_id='".$mdrand."' WHERE email='".$username."'");
echo "<p>Welcome back ".$username."! Click <a href='profile.php'>here</a> to view your profile!</a></p>";
if(!$query) {
echo "<p>Oops, this is strange ... we cannot seem to log you in at the moment! Please try again in 5 minutes. If this problem occurs again, please contact our support department at <a href='mailto:support@example.com'>support@example.com</a></p>";
}
}
// THIS IS TRUE IF THE USERNAME AND PASSWORD DOES NOT MATCH
if($num != 0) {
echo "<p>The username and password you entered does not exist or your account needs to be verified. Please check your details and try again. | <a href='index.php'>TRY AGAIN</a> | <a href='forgotpass.php'>FORGOT MY PASSWORD</a> | <a href='register.php'>REGISTER A FREE ACCOUNT</a></p>";
}
// THIS IS TRUE IF THE USER DID NOT PRESS SUBMIT. THIS JUST SHOWS THE LOGIN FORM
} else {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
Email: <input type="text" name="email" />
Password: <input type="text" name="password" />
Remember me:<input name="rememberme" type="checkbox" value="remember" /></input>
<input name="userlogin" type="submit" value="submit"></input>
| Forgot my password
</p>
</form>
<?php
}
?>
[edited by: eelixduppy at 3:07 pm (utc) on Feb 11, 2010]
[edit reason] exemplified [/edit]
if($_SESSION["id"] == "1") if($_SESSION["active"] == "1") $uname = $_SESSION['username']; $_SESSION['validuser'] = true;
$_SESSION['username'] = $_POST['email']; <?php
if ($_SESSION['validuser']!==true) { include('my_login_page.html'); exit(); }
?>
(page for member zone is down here)
<?php
if($_SESSION["active"] == "1")
{
echo "<p>Welcome back ".$result["name"]."! <a href='news.php'>News</a> |<a href='profile.php'>Profile</a> |<a href='logout.php'>Logout</a></p>";
}
if($_POST["userlogin"] == "submit") {
$username = $_POST["email"];
$password = substr(md5($_POST["password"]),0,16);
connectdb();
$query = mysql_query("SELECT name, email, password, account_type FROM ov_users WHERE email = '".$email."' AND password = '".$password."' AND account_type = '2'");
if(!$query) {
echo "<p>Oops, this is strange ... we cannot seem to log you in at the moment! Please try again in 5 minutes. If this problem occurs again, please contact our support department at <a href='mailto:support@outdoorvillage.co.za'>support@outdoorvillage.co.za</a></p>";
}
$num = mysql_num_rows($query);
while($result = mysql_fetch_array($query)) {
$uname = $result["name"];
}
// This indicates that the user logged in successfully but did not select to remember me.
if($num > 0) {
$_SESSION["username"] = $username;
$_SESSION["id"] = session_id();
$_SESSION["active"] = "1";
echo "<p>Welcome back ".$username." Click <a href='profile.php'>here</a> to view your profile!</a></p>";
}
// This indicates that the user logged in successfully but selected to be remembered.
if($num > 0 && $rememberme == "remember") {
setcookie("username",$username,time()+30754400);
$rand = rand(0,1000000);
setcookie("cookie_id",$rand,time()+30754400);
$mdrand = md5($rand);
$query = mysql_query("UPDATE ov_users SET cookie_id='".$mdrand."' WHERE email='".$username."'");
if(!$query) {
echo "<p>Oops, this is strange ... we cannot seem to log you in at the moment! Please try again in 5 minutes. If this problem occurs again, please contact our support department at <a href='mailto:support@outdoorvillage.co.za'>support@outdoorvillage.co.za</a></p>";
exit();
}
echo "<p>Welcome back ".$username."! Click <a href='profile.php'>here</a> to view your profile!</a></p>";
}
}
if($num != 0) {
echo "<p>The username and password you entered does not exist or your account needs to be verified. Please check your details and try again. | <a href='index.php'>TRY AGAIN</a> | <a href='forgotpass.php'>FORGOT MY PASSWORD</a> | <a href='register.php'>REGISTER A FREE ACCOUNT</a></p>";
}
if($_SESSION["active"] != "1")
{
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
Email: <input type="text" name="email" />
Password: <input type="text" name="password" />
Remember me:<input name="rememberme" type="checkbox" value="remember" /></input>
<input name="userlogin" type="submit" value="submit"></input>
| Forgot my password
</p>
</form>
<?php
}
?>
$username = $_POST["email"];
$username = $_POST['email'];
and the $_SESSION['active'], $_SESSION['id'],$_SESSION['username']
if(isset($_SESSION['active']) && ($_SESSION['active'] == "1"))
{
//its set, do stuff - etc
}