Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem in creating a simple login system..
i am just fetching the username and pwd form database.. and if username and pwd is correct.. i am just trying to redirect to the other page to show that

welcome username

here is my login page code

PHP
<?php

$host = 'localhost'; 
$user = 'root'; 
$password = ''; 
$db = 'dbsneaker'; 

$link = mysql_connect($host,$user,$password) or die('Error in Server information');
mysql_select_db($db,$link) or die('Can not Select Databasse');



$userName = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']);


$query = "select * from tbladmin where admin_usr_name='$userName' and admin_pwd='$password'";

$res = mysql_query($query);

$rows = mysql_num_rows($res);


if($rows==1)


{
   //echo "login successful";
$_SESSION['userName']=$userName;    // please take a look on session vairiable please
$_SESSION['password']=$password;
header("location:success.php");
}
else
{
echo 'wrong Username or password';
}

?>


here is the code for success.php page

PHP
<?php
session_start();
if($_SESSION['userName']!='')
{
header("location:login_form.php");

}
else
{
echo '<h2>Successfully Login <br /> </h2>';
echo "hello=". $_SESSION['userName'];

echo '<a href="logout.php"> Log Out</a>';
}

?>


i am getting these errors

( ! ) Notice: Undefined index: userName in C:\wamp\www\login sys\success.php on line 3
Call Stack
# Time Memory Function Location
1 0.0006 671144 {main}( ) ..\success.php:0
Successfully Login

( ! ) Notice: Undefined index: userName in C:\wamp\www\login sys\success.php on line 11
Call Stack
# Time Memory Function Location
1 0.0006 671144 {main}( ) ..\success.php:0
Posted

1 solution

What are you wondering?
First of all, you don't start the session before adding username and password (why on earth are you adding the password to the session after all?). You need to uses session_start in any script that is accessing session state, before accessing session state (of course take account of the includes).
Secondly, if you want to look at an array element, and you don't suppress this level of error (see: http://php.net/manual/en/function.error-reporting.php[^]), you have to check the existence of the key to avoid such messages, and by the way you have to swap if and else branches to give them sense:
PHP
<?php
session_start();
if(isset($_SESSION['userName']) && !empty($_SESSION['userName']))
{
    echo '<h2>Successfully Login <br /> </h2>';
    echo "hello=". $_SESSION['userName'];
    echo '<a href="logout.php"> Log Out</a>';
}
else
{
    header("location:login_form.php");
}
?>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900