Topic: [HACK] Use CuteNews' users.db.php on other pages
Name: A secure-page for added users in CN
Author: raee & Jetski
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: I just made a login-script which checks against the users.db.php in cutenews/data/, and makes a session cookie.
This is very usefull if you want to have a page with info, only for users allready added in Cutenews.
It can also be edited, so only f.x users with editor-status has access.
I thought it was pretty usefull.
Requirements:
Demo:
Discussion Topic:
Instructions:
In a file called auth.inc.php have the following
<?PHP
session_start();
// authenticate username/password against /cutenews/data/users.db.php
// returns: -1 if user does not exist
// 0 if user exists but password is incorrect
// 1 if username and password are correct
function auth($user, $pass){
$result = -1;
if((trim($user) != "") && (trim($pass) != "")){
// make sure that the script has permission to read this file!
$data = file("data/users.db.php");
// iterate through file
foreach ($data as $line){
$arr = explode("|", $line);
// if username matches
// test password
if($arr[2] == $user){
// if match, user/pass combination is correct
// return 1
if($arr[3] == $pass){
$result = 1;
break;
}else{
// otherwise return 0
$result = 0;
break;
}
}
}
}
// return value
return $result;
}
// Check if Sessions have exist or else see if any var's are posted
if(!isset($_SESSION["SESSION_UNAME"]) && !isset($_SESSION["SESSION_UPASS"])){
$f_user = $_POST['f_user'];
$f_pass = md5($_POST['f_pass']);
}else{
$f_user = $_SESSION["SESSION_UNAME"];
$f_pass = $_SESSION["SESSION_UPASS"];
}
if($_GET['logout'] == "true"){
$f_user = "";
$f_pass = "";
session_unset();
session_destroy();
header("Location: ?");
}
if(auth($f_user, $f_pass) == 1){
$_SESSION["SESSION_UNAME"] = $f_user;
$_SESSION["SESSION_UPASS"] = $f_pass;
}else{
echo <<<HTML
<html>
<head>
<title>Login</title>
</head>
<body>
<center>
<table border="0" cellspacing="5" cellpadding="5">
<form action="" method="POST">
<tr>
<td>Username</td>
<td><input type="text" size="20" name="f_user"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" size="20" name="f_pass"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value=" LogIn ">
</td>
</tr>
</form>
</table>
</center>
</body>
</html>
HTML;
exit();
}
?>
Then just use
<?PHP include("auth.inc.php"); ?>
at the top of the pages you want to protect.
Unless add this link code
[url=?logout=true]logout[/url]
somewhere on the page soo people can logout.
Also make sure in auth.inc.php
file("data/users.db.php");
has the currect path to the users.db.php file.
If it happens you're logged out after visitting 1 page, move the session_start() function to the top of the main php file. with main i mean where you're including the hack. The file where you move session_start() to may not be included, it must be the 'control' file.