1

Welcome Notice: Undefined variable: myusername in C:\xampp\xamp\New folder\htdocs\login-form\login\login_success.php on line 6 no *

thats the error of my project when i tried to use this code. I want to add Welcome (username). I think theres an error in my login_success but i cant figure it out. Help me please

--------------checklogin.php---------------------------

<?php

ob_start();
$host="localhost";
$username="root";
$password="root";
$db_name="test";
$tbl_name="members";

mysql_connect($host,$username,$password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$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);

$count=mysql_num_rows($result);

if($count==1){

$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;

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

else{

echo "Wrong Username or Password";
}
ob_end_flush();
?>

------------------login_success.php----------------------------

<li><a href="#">Welcome 
<?php  
session_start();
$_SESSION['myusername'] = $myusername;


if(isset($_SESSION['myusername'])) 
  { 
  echo "ok"; 

  }  
else 
   { 
     echo "no"; 
     } 
?>  
  • `$myusername` is undefined, the error message is pretty straight forward. Passwords should be hashed.. – chris85 Jul 24 '16 at 03:41
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85 Jul 24 '16 at 03:42
  • You also need `session_start()` in your check login script. – chris85 Jul 24 '16 at 03:51
  • I tried to put session_start() and make it $myusername = $_SESSION['myusername'] but it says "No user is logged in." help mee please. I think theres a problem in my if else or in "$myusername = $_SESSION['myusername']" –  Jul 24 '16 at 03:55

3 Answers3

1

You need to check this line in login_success.php

$_SESSION['myusername'] = $myusername;

Here, you have already stored $_SESSION['myusername'] in the previuos page after login. This line indicates you're trying to overwrite the $_SESSION['myusername'] with $myusername.

But, our objective is to read $_SESSION['myusername'] and store it in $myusername. So, simply interchange the sides:

$myusername = $_SESSION['myusername']

Try this:

session_start();
$myusername = $_SESSION['myusername'];

if (isset($_SESSION['myusername'])) { 
    echo "User".$username." is logged in"; 
}  else { 
    echo "No user is logged in."; 
 } 

-checklogin.php--

$sql = "SELECT * FROM $tbl_name WHERE Username='$myusername' and password='$mypassword'";

$result = mysql_query($sql) or die(mysql_error());  // Check if you get any SQL syntax error
$count = mysql_num_rows($result); 
var_dump($count); // Check the value of $count
exit;
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • Please take a look at my edited answer and change your check_login page accordingly. Let me know the message you get. Maybe your SQL has some syntax error. – Indrasis Datta Jul 24 '16 at 03:56
0
$_SESSION['myusername'] = $myusername;

You are trying to assign value of $myusername to $_SESSION['myusername'].

But you have to do opposite thing.

$myusername = $_SESSION['myusername'];
Sailesh Babu Doppalapudi
  • 1,534
  • 1
  • 10
  • 22
  • i tried that but it says "No use is logged in" I think theres a problem help me please –  Jul 24 '16 at 03:57
0
session_start();
$myusername = $_SESSION['myusername'];

if (isset($_SESSION['myusername'])) { 
    echo "User".$username." is logged in"; 
}

omit the else part if you want nothing to be displayed if $_SESSION['myusername'] is null/empty.

Blue
  • 22,608
  • 7
  • 62
  • 92
jarvo69
  • 7,908
  • 2
  • 18
  • 28
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 24 '16 at 14:53