这是我的代码......
loginscript.php
<?php
ini_set('display_errors', true);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="seelsdb"; // Database name
$tbl_name="tblteacher"; // 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");
// username and password sent from form
$uname=$_POST['uname'];
$pword=$_POST['pword'];
// To protect MySQL injection (more detail about MySQL injection)
$uname = stripslashes($uname);
$pword = stripslashes($pword);
$uname = mysql_real_escape_string($uname);
$pword = mysql_real_escape_string($pword);
$sql="SELECT * FROM $tbl_name WHERE teacherEmail='$uname' and teacherPass='$pword'";
$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_start();
$_SESSION['loginp']=$pword;
$_SESSION['login']=$uname;
header("location:schedule.php");
}
else {
echo "Wrong Username or Password";
}
?>
这是一个schedule.php的片段
<?php
session_start();
$uname=$_SESSION['login'];
echo $uname;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body class="body">
<table class="maintable">
<tr valign="top">
<td align="center">
<img src="images/banner.jpg" />
</td>
</tr>
<tr>
<td>
<!-- this part is for the menu area -->
</td>
</tr>
</table>
<table>
<tr valign="top">
<td width="20%">
<!-- this part is for the login part -->
<table>
<tr>
<td>
<!-- i want to display $_SESSION['login'] here -->
WELCOME! $uname
</td>
</tr>
</table>
现在,如何从loginscript.php获取$ _SESSION ['login']的值并将其显示在schedule.php中的一个表格单元格中
我收到一条错误,上面写着通知:未定义索引:登录
答案 0 :(得分:0)
很简单,您应该使用PHP
WELCOME! <?php echo $uname; ?>
另一方面,在第一个文件(loginscript.php
)中,您应该在session_start()
之前添加<?php
,因为您有ini_set('display_errors', true);
并且会破坏您的会话。
答案 1 :(得分:0)
首先,您应该在loginscript.php中发送标头之前启动会话。其次你应该打印登录。在源代码中,您应该在loginscript.php中添加session_start();
行(这应该是第一行),在HTML代码中打印用户名时,您应该代替echo hello $uname
写<? echo"hello $uname"; ?>
答案 2 :(得分:0)
使用;
WELCOME! <?php echo $uname; ?>
代替