PHP code for login with different subpages

August 17, 2011 at 02:32:18
Specs: Windows 7
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:

-table in Mysql dtb

surname pass town
Mrkva 123 A
Slana 456 B



$dbhost= 'localhost';
$dbuser= 'root';
$dbpassword= '';

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' ";
$count=mysql_num_rows($result) ;



else {



See More: PHP code for login with different subpages

Report •

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.

Report •

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


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:

if (!session_is_registered('surname')){

Report •

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["surname"] = $_POST["user"];
    $_SESSION["passwrd"] = $_POST["pass"];
    header("location: " . $dbresult["town"]);
} else {


// town.php

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.


Report •

Related Solutions

Ask Question