我正在尝试Echo $ _SESSION [“myusername”]。我可以登录我的页面,但我不能Echo myusername。我收到以下错误。 未定义的变量:C:....中的myusername
checklogin.php页面如下
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="techdb"; // 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");
$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);
// 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){
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;
header("location:technicianform.php");
} else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
我的technicianform.php页面是
<?php
session_start();
if( isset($_SESSION['$myusername']) ){
header("location:checklogin.php");
}
?>
<html>
<body>
<p>Login Successful</p>
<?php echo $_SESSION["myusername"]; ?>
<p><br/>
<br/><br/><a href='Logout.php'>Click here to log out</a></p>
</body>
</html>
登录过程有效但我似乎无法回显myusername。非常感谢您的帮助。
答案 0 :(得分:0)
要在checklogin.php
文件中创建会话变量,您需要开始会话。
您正在获取未定义的索引通知,因为您缺少以下内容:
session_start(); // add in checklogin.php file
第二个问题是:
$_SESSION['$myusername']
应该是:
$_SESSION['myusername']
旁注:
停止使用mysql_*
扩展名,在PHP 7中弃用并关闭。使用mysqli_*
或PDO
。
答案 1 :(得分:0)
只需删除此$符号。
变化:
if( isset($_SESSION['$myusername']) )
要
if( isset($_SESSION['myusername']) )
另外,在checklogin.php中你需要提到
session_start();
希望这有帮助。
和平!的xD
答案 2 :(得分:0)
**Try This.**
login.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="techdb"; // 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");
if(isset($_POST['submit'])){
$myusername=trim($_POST['myusername']);
$mypassword=trim($_POST['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){
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;
header("location:technicianform.php");
}
else {
echo "Wrong Username or Password";
}
}
?>
<form method="post">
<label>username :</label><input type="text" name="myusername"></br>
<label>password :</label> <input type="password" name="mypassword"></br>
<input type="submit" name="submit" value="submit">
</form>`
technicianform.php
<?php
session_start();
if( isset($_SESSION['$myusername']) ){
header("location:checklogin.php");
}
?>
<html>
<body>
<p>Login Successful</p>
<?php
echo $_SESSION["myusername"];
?>
<p><br/>
<br/><br/><a href='Logout.php'>Click here to log out</a></p>
</body>
</html>