PHP code for login with different subpages

August 17, 2011 at 02:32:18
Specs: Windows 7
Hello,
I would like to ask for help with PHP code for login. I need that user who is already loged in was redirected with his parameter TOWN, which has this user in mysql dtb to the subpage town.php. I am sorry I am beginner with PHP :-).
After log in Mrkva will go to A.php and Slana to B.php. Thank you very much. My e-mail is: wikinka@azet.sk


-table in Mysql dtb

surname pass town
Mrkva 123 A
Slana 456 B

-php

<?php
ob_start();

$dbhost= 'localhost';
$dbuser= 'root';
$dbpassword= '';
$db='doc';
$tbl='tbl';

mysql_connect($dbhost,$dbuser,$dbpassword) or die("cannot connect");
mysql_select_db($db) or die("cannot select DB");

$surname = $_POST['surname'];
$pass = $_POST['pass'];

$sql="SELECT * FROM $tbl WHERE surname='$surname' and pass='$pass' ";
$result=mysql_query($sql);
$count=mysql_num_rows($result) ;

if($count==1){
session_register("surname");
session_register("pass");

header("location:A.php");}

else {
header("location:index.php");
}

ob_end_flush();

?>



See More: PHP code for login with different subpages

Report •

#1
August 22, 2011 at 06:55:01
If the surname, password, and town are stored in the dbase, then use town's value to redirect. For example,

$res = mysql_query("select * from $tbl where surname=$surname and pass=$pass limit 1;");

if(mysql_num_rows($res) == 1) {
   $row = mysql_fetch_assoc($res));
    header("location: " . $row['town'] . ".php");
} else {
   // user provided details didn't match stored records
}

not tested, but maybe something like that.
HTH


Report •

#2
August 23, 2011 at 02:47:36
Thank you very much :-)) its working. Still I need to protect the site not to enter without login. I tried

session_register('surname');

but it doesnt work. If I wrote 'town'.php without login I was unfortunately trasfered to 'town'.php :-((( Pls help me.


In the 'town'.php I have:

session_start();
if (!session_is_registered('surname')){
header("location:login.php");
}


Report •

#3
August 29, 2011 at 19:36:25
Not sure what your town page is doing, but in your login page, if your user is in the database, try something like

according the php doc, session_register is deprecated for versions over 4.1 and suggests using $_SESSION instead.

// login.php
   other bits of code

if(user exists) {
   session_start();
    $_SESSION["surname"] = $_POST["user"];
    $_SESSION["passwrd"] = $_POST["pass"];
    header("location: " . $dbresult["town"]);
} else {
   ...
}

...

// town.php

session_start();
if(session_is_registered("surname")) {
   foreach($_SESSION as $key => $val) {
      echo "\$_SESSION[$key] = $val <br />";
   }
} else {
   header("location: login.php");
}

If the login session is ok, it *should* presist as long as the browser is open. If you tried to load town.php without having used the login page to create a session, it *should* send you to the login page.

HTH


Report •
Related Solutions


Ask Question