2

i am trying to have a login script burt i have this error Undefined variable: _session see below the pages

//checklogin.php

<?php

ob_start();
$host="localhost"; // Host name 
$username="xxxxx"; // Mysql username 
$password="xxxx"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.

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

// Define $myusername and $mypassword 

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

// To protect MySQL injection (more detail about MySQL injection) 

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

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_session['name']= myusername;
$_session['pass']= mypassword; 
header("location:login_success.php");
}
 else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

//login_success.php

// Check if session is not registered, redirect back to main page. 
// Put this code in first line of web page. 
<?php
session_start();
if(!$_session['name']= myusername){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>
2
  • 2
    You need to capitlize it - it's $_SESSION Commented Sep 26, 2012 at 15:10
  • also if(!$_session['name']= myusername) should probably be if($_session['name'] != myusername) Commented Sep 26, 2012 at 15:13

2 Answers 2

3

It is $_SESSION not $_session you also need to add session_start() on top of the page

FROM PHP DOC

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Sign up to request clarification or add additional context in comments.

2 Comments

he has session_start() there.
@mistrfu: Not in checklogin.php
2

$_SESSION is to be written capitalized. In your code it is all lower case:

if(!$_session['name']= myusername){

change to (also add the $ to myusername):

if(!$_SESSION['name']= $myusername){

6 Comments

thanks everyone u are great my mistake sorry , but now i have this Use of undefined constant myusername - assumed 'myusername' in /Applications/MAMP/htdocs/test/login_success.php on line 5
@LefterisLivanos Add the $ to myusername to mark it as the variable you defined before. Alternatively you can make it a constant and define it by using define() somewhere.
i did what u told me and now the code is : // Check if session is not registered, redirect back to main page. // Put this code in first line of web page. <?php session_start(); if(!$_SESSION['name']=$myusername{ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html>
but now i have this error: Parse error: syntax error, unexpected ';' in /Applications/MAMP/htdocs/test/login_success.php on line 6
@LefterisLivanos I have none at hand. Just Google one. There should be plenty of them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.