Deceptive-Logic


Copy this code, make the neccessary changes, save as a .php file, upload to your server, and use <? include('filename.php'); ?> where ever you want it to show up (change filename.php to the files acctual name).

If you have any questions, just ask on the tutorial help forums.

This is the form that you'll need to use everything, copy it to your index or save it as a seperate file from the php code

<form method=POST action="login.php"> <input type="text" name="username" value="Username" onFocus="select();"><br> <input type="password" name="user_pass" value="Password" onFocus="select();"><br> <input type="submit" name="submit" value="Login"> </form>

This will be a seperate .php file from the form, do not include it on your index in anyway but instead redirect from this page to your index after logging in (link in script to do so).
According to the form, this file should be named login.php.

<?
/* kills the script if the submit button wasn't pressed */
if(!$_POST['submit']){
    die(
"Hacking attempt");
}

#database info, or include a config.php file
$db_host 'localhost'#change to your host
$db_user 'username'#change to your user name
$db_password 'password'#change to your password
$db_name 'database_name'#change to your db name

/* Connects to the the database, and selects the database, or kills the script and displays an error that you'll be able to work with */
@mysql_connect($db_host,$db_user,$db_password) or die("Unable to connect to database on line <b>".__LINE__."</b> in <b>".__FILE__."</b>");
@
mysql_select_db($db_name) or die("Unable to select to database on line <b>".__LINE__."</b> in <b>".__FILE__."</b>");

/* cleans up the username and pass to work with the database, and to help prevent fraud logins */
$name trim($_POST['username']);
$password md5(trim($_POST['user_pass']));

/* kills the script if a username and pass were not entered */
if(!$name || !$password){
    die(
"You need to enter both a username and password before continuing.");
}

/* Searches the database for a user whos id is > 0, change the table prefix if neccessary */
$result mysql_query("SELECT * FROM phpbb_users WHERE user_id != -1 AND username = '$name' LIMIT 1");

/* Loops through the result to match a user and password, then starts a session to keep them logged in */
while($r=mysql_fetch_assoc($result)){
    if((
$r['username'] == $name) && ($r['user_password'] == $password) && ($r['user_active'])){
        
/* Sets the session's name. */
        
session_name("sid");
        
/* Starts the session */
        
session_start();
        
/* Gets the current session name. */
        
$s_name session_name();
        
/* Gets the current session id, used to track the user from page to page */
        
$s_id session_id();
        
/* Sets the username in the session to be passed from page to page. */
        
$_SESSION['username'] = $name;
        
/* Get the current user id, for later use. */
        
$user_id $r['user_id'];
        
/* Get the current time, for later use */
        
$cur_time time();
        
/* Get the user IP, and format it the same way phpBB does (hopefully) */
        
$user_ip md5(uniqid($_SERVER['REMOTE_ADDR']));
    }elseif(!
$r['user_active']){
        
/* kills the script if the user has not been activated */
        
die("Sorry, but you must be activated before you can login");
    }else{
        
/* kills the script if something didn't match up above */
        
die("Invalid username or password.  Please try again.");
    }
}

/* Checks to make sure the session's username was set, or kills the script */
if(empty($_SESSION['username'])){
    die(
"Username was not set, please try again or contact the site admin.");
}

/* Query used to update the phpBB database, change the prefix of your table if neccessary */
mysql_query("INSERT INTO phpbb_sessions(session_id, session_user_id, session_start, session_time, session_ip, session_logged_in)
VALUES ('$s_id', '$user_id', '$cur_time', '$cur_time', '$user_ip', '1')"
);

/* Close the mysql connection to help prevent hacking */
mysql_close();

/* This part simply says hi and gives a link back to the index */
echo "You are now logged in as ".$_SESSION['username']."<br>
<a href=/index.php?"
.$s_name."=".$s_id.">Return Home</a>.";
?>

Now that you're all nice and logged in, how to stay logged in.
First off, you need to change all of your links, to contain the $sid variable, to do so, simply add this to the top of your index.

<?
/* starts a session with the name "sid", used to keep us logged in as long as there is a $sid variable in the url, which we are going to set up next */
session_name("sid");
session_start();

/* this is a simple function that adds the users sid to a url, it's based on the function that phpBB forums acctually use */
function add_sid($url){
    
/* sets some variables to current sessions name and id */
    
$s_name session_name();
    
$s_id session_id();
    
$sid $s_name."=".$s_id;
    if(!
defined('LOGGED')){
        
/* regular url used if the user is not logged in */
        
return $url;
    }else{
        
/* checks to see if the url already contains the session's variable */
        
if(preg_match("#$s_name=#"$url)){
            
/* if it does, return the url */
            
return $url;
        }
        
/* this is used if the url doesn't have the proper variable, and the user is logged in
        it formats the url using something similar to an if statement
        visit http://php.net/manual/en/language.expressions.php for more info */
        
$url .= ((strpos($url'?') !== false) ? '&' '?') . $sid;

        
/* returns the properly formated url */
        
return $url;
    }
}
?>

Now, how do you use the function? Simply add this below the function, and make the neccessary changes.

<?
/* defines a logged in users name, and sets a constant that says they are logged in */
if(!empty($_SESSION['username'])){
    
/* Sets the username according to what the session stored */
    
$username $_SESSION['username'];
    
/* defines the constant LOGGED, use this later */
    
define('LOGGED'true);
}else{
    
/* default username */
    
$username "Guest";
}
/* optional welcome statement */
echo "Welcome $username.";

/* this is a sample link, you don't need it, its here for educational purposes. */
echo "<a href=".add_sid("/short/link/to_page.php").">DISPLAYED_PART</a>";
?>

Ok, we defined a nice constant, and set some variables up, all for what you ask? Well, heres something you can do with the constant (i.e. for members-only features?).

<?
if(defined('LOGGED')){
    echo 
"You get members only features. YAY!";
}else{
    echo 
"Hey, go register and login for members only features!!";
}
?>

There a lot more you can do with this, just use your imagination.
Note: Chances are, your users will not stay logged in when switching from your main site, to the forums. I'm working on a way to fix that.