// # How to use the function pml_login()?
// pml_login([what to do (include|redirect)],[which page])
// When a user is logged in, a message will be displayed that the user is logged in. If you want to redirect the user to a page,
// or if you want to include a page (p.e. a menu), then you can use the function on another way.
//# To INCLUDE A PAGE when the user is LOGGED IN use:
// pml_login('include','path-to-include.php');
// Where pagetoinclude.php the page is which should be included (tip: check on the included page if the user is logged on!)
// When using include, you have to referer to the page from the folder where the PhpMyLogon file is placed. If you have for example PhpMyLogon placed in the folder
// 'pml', and you use it from the root, you have to use ../filename.php if the file is also in the root folder.
// # To REDIRECT when the user is LOGGED IN use:
// pml_login('redirect','redirecttothispage.php');
// Where redirecttothispage.php the page is where the user will be redirected to (tip: check on the redirected page if the user is logged on!)
// When using redirect, you should referer to the page as you should do when you would redirect it from the page where you include PhpMyLogon. This is different then when
// you use the include function!!
function pml_login($todo = "",$action = "") {
ob_start();
include("lang.php");
include("pml_config.inc.php");
if(!isset($_SESSION)) { exit($lang['sessionproblem']); }
pml_connect();
// Check if user is logged in
if(!isset($_SESSION['pml_userid'])) {
if(isset($_COOKIE['pml_userid_cookie'])) {
// Check cookie data with data in database
$sql = "SELECT id,username,password,cookie_pass,actcode,rank FROM `".$settings['db_table']."` WHERE id = '".$_COOKIE['pml_userid_cookie']."' LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 1) {
// User exists
$row = mysql_fetch_array($query);
$id = htmlspecialchars($row['id']);
$username = htmlspecialchars($row['username']);
$password_db = htmlspecialchars($row['password']);
$cookie_pass = htmlspecialchars($row['cookie_pass']);
$actcode = htmlspecialchars($row['actcode']);
$rank = htmlspecialchars($row['rank']);
if($actcode == "") {
// Useraccount is activated
if($cookie_pass == $_COOKIE['pml_usercode_cookie']) {
// Everything ok, create sessions
$_SESSION['pml_userid'] = $id;
$_SESSION['pml_userrank'] = $rank;
$sql_updateonline = "UPDATE `".$settings['db_table']."` SET lastactive = NOW() AND lastlogin = NOW() WHERE id = '".$id."' LIMIT 1";
mysql_query($sql_updateonline);
header("Location: ".$_SERVER['REQUEST_URI']);
}else{
// Incorrect password
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}else{
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}else{
// User doesn't exists
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}
if(isset($_POST['submit'])) {
if($_POST['username'] != "" AND $_POST['password'] != "") {
// Check submitted data with data in database
$sql = "SELECT id,username,password,cookie_pass,actcode,rank FROM `".$settings['db_table']."` WHERE username = '".$_POST['username']."' LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 1) {
// User exists
$row = mysql_fetch_array($query);
$id = htmlspecialchars($row['id']);
$username = htmlspecialchars($row['username']);
$password_db = htmlspecialchars($row['password']);
$cookie_pass = htmlspecialchars($row['cookie_pass']);
$actcode = htmlspecialchars($row['actcode']);
$rank = htmlspecialchars($row['rank']);
if($actcode == "") {
// Useraccount is activated
if($password_db == sha1(md5($_POST['password']))) {
// Everything ok, create sessions
$_SESSION['pml_userid'] = $id;
$_SESSION['pml_userrank'] = $rank;
if(isset($_POST['cookie'])) {
// Also create cookie
setcookie("pml_userid_cookie", $id, time() + 365 * 86400);
if($cookie_pass == "") {
// Create cookie code
mt_srand((double)microtime()*1000000);
$pass = 1;
while(strlen($pass) <= 10) {
$i = chr(mt_rand(0,255));
if(eregi("^[a-z0-9]$",$i)) {
$pass = $pass.$i;
}
}
$cookie_pass = md5($pass);
$sql_cookiepass = "UPDATE `".$settings['db_table']."` SET cookie_pass = '".$cookie_pass."' WHERE id = ".$id." LIMIT 1";
mysql_query($sql_cookiepass);
}
setcookie("pml_usercode_cookie", $cookie_pass, time() + 365 * 86400);
}
$sql_updateonline = "UPDATE `".$settings['db_table']."` SET lastactive = NOW(),lastlogin = NOW() WHERE id = '".$id."' LIMIT 1";
mysql_query($sql_updateonline) or trigger_error(mysql_error());
header("Location: ".$_SERVER['REQUEST_URI']);
}else{
// Incorrect password
echo $lang['login-incorrect']."<br />";
}
}else{
echo $lang['login-notactive']."<br />";
}
}else{
// User doesn't exists
echo $lang['login-incorrect']."<br />";
}
}else{
echo $lang['login-forgotfield']."<br />";
}
}
// Login form
echo "\n";
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table width="272">
<tr>
<td width="264"><?php echo $lang['login-username']; ?></br></td>
</tr>
<tr>
<td>
<input type="text" id="username" name="username" <?php if(isset($_POST['username'])) { echo 'value="'.$_POST['username'].'"'; } ?> /></td>
</tr>
<tr>
<td><label for="cookie"><?php echo $lang['login-password']; ?></label></td>
</tr>
<tr>
<td><input type="password" id="password" name="password" <?php if(isset($_POST['password'])) { echo 'value="'.$_POST['password'].'"'; } ?> /></td>
</tr>
<tr>
<td><input type="checkbox" id="cookie" name="cookie" value="true" <?php if(isset($_POST['cookie'])) { echo "checked"; } ?> />
<?php echo $lang['login-cookie']; ?></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="<?php echo $lang['login-submitbutton']; ?>" /></td>
</tr>
</table>
</form>
</li>
<li>
<a href="?page=registreer">Registreren</a>
<?php
}else{
// User is logged on, redirect to page $goto; if no $goto just view msg that user is logged in
if($todo != "") {
if($todo == "include") {
include($action);
}elseif($todo == "redirect") {
header("Location: ".$action);
}else{
echo $lang['functionproblem'];
}
}else{
echo $lang['login-already'];
}
}
ob_end_flush();
}