Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
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
 
$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
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.phpBlush | :O
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.phpBlush | :O
Posted 20-Apr-13 8:51am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
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");
}
?>
  Permalink  
v3

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100