Login page & index.php

Hi There

I have a very simple login script , my problem is that I have no idea how to display my [b]index.php[/b] once the user has successfully logged in. Please will someone be able to assist. Thank you for all your help so far , it is much appreciated.

Regards.

[b]main_login.php[/b]
Member Login
Username :
Password :
   
[b]
checklogin.php[/b]

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

[b]login_page.php[/b]

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
[b]
login_success.php[/b]
?>



Login Successful



[b]logout.php[/b]

<?
session_start();
session_destroy();
?>

Comments

  • : I have a very simple login script , my problem is that I have no
    : idea how to display my [b]index.php[/b] once the user has
    : successfully logged in. Please will someone be able to assist. Thank
    : you for all your help so far , it is much appreciated.

    You can redirect there from your login_success.php. You can either redirect using header(), or if you want to show the 'login successful' first, use a HTML META tag with REFRESH attribute.
  • One option would be to use the index.php as just a controller page and then pass parameters (current_page) to this page in the post or get fields.
    $_REQUEST["current_page"] = "{what ever page u want to go to}"
    the index.php would

    have function that check that the user is logged in (using a session)

    $task = check_user_login();

    $d_file = sprintf($file_path."%s.php",$task);

    if(file_exists($d_file)){
    include $d_file;
    }else{
    include($file_path."php_files/error.php");
    }
    exit(0);
    function check_user_login(){
    if(isset($_SESSION[user_token])){
    $task = $task // what ever task was passed in in the first place
    }else{
    $task= logout; // destroy all session values
    }

    if(!allowed_actions()){ // always good to check if the current user has permission to perform the task or view the page.
    task = logout;
    }
    return task;
    }

    so for ur login, $_REQUEST["current_page"] = "login". after the login form has been processed, u just redirect to the index.page using ur redirect function.

    One more thing after u redirect a page, always exit(); so it would be best to wrap the header(location:) inside a function.

    Hope this help, I know it might not be too clear. send me an email if you want more clarification.

    Omo
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories