PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Login page & index.php Posted by munky344 on 25 Oct 2008 at 11:22 AM
Hi There

I have a very simple login script , my problem is that I have no idea how to display my index.php 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.

main_login.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

checklogin.php


<?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();
?>

login_page.php

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}

login_success.php

?>

<html>
<body>
Login Successful
</body>
</html>

logout.php

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

Report
Re: Login page & index.php Posted by pritaeas on 3 Nov 2008 at 4:34 AM
: I have a very simple login script , my problem is that I have no
: idea how to display my index.php 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.
Report
Re: Login page & index.php Posted by Agbagbara on 20 Nov 2008 at 6:24 PM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.