Index.php display different for logged in users?

Hello, I’ve been making a site for one of my friends and I’m having trouble figuring out how to display different pages for logged in users.

Here’s my index.php
[php]<?php
require (‘connect.php’);
include ‘header.php’;

$form = <<<EOT

Username:
Password:

Login

Don't have an account? Register here!
EOT; ?>[/php]

And here’s my login.php
[php]<?php
require(‘connect.php’);
include ‘header.php’;
if(isset($_POST[‘submit’])){
$user = mysql_escape_string($_POST[‘user’]);
$pass = mysql_escape_string($_POST[‘pass’]);
$pass = md5($pass);

$sql = mysql_query("SELECT * FROM `users` WHERE `user` = '$user' AND `pass` = '$pass'");
if(mysql_num_rows($sql) > 0) {
	echo '<div id="userloggedin">';
	exit();
	}else {
		echo '
			<div id="login">
Wrong username or password Username:
Password:
'; } }else{

}

$form = <<<EOT

Username:
Password:

Login

EOT;

echo $form;
?>[/php]

Thanks!

once your user has logged in you would do something like:
[php]if(isset($_SESSION[‘logged_in’])) {
// content here…
}
else {
$url = ‘/index.php’;
Header(“Location: $url”);
exit();
}[/php]
what this code does is check there is a session called ‘logged_in’.
If there is one set, it will show the content, if there isn’t one set then it will redirect the user to the index page. You can of course change this redirect to any page you like IE back to the login page.

Hope this helps,
Red :wink:

or you could just put the following on top of you members page

[php] // First we execute our common code to connection to the database and start the session
require(“includes/common.php”);

// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
    // If they are not, we redirect them to the login page.
    header("Location: index.php");
    
    // On a production website it isn't wize to use the following:
    die("Redirecting to index.php");
}[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service