将数据从php脚本文件传输到另一个脚本文件


transfering data from a php script file to another

这是我的代码。。。

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中的一个表单元格中???

我收到一个错误,上面写着:注意:未定义的索引:登录

第一,您应该在loginscript.php中发送头之前启动会话。第二,您应该像打印登录名一样打印。在源代码中,您应该将session_start();行添加到您的loginscript.php中(这应该是第一行),并且在HTML代码中打印用户名时,您应该代替echo hello $uname编写<? echo"hello $uname"; ?>

simple,您应该使用PHP 打印变量

WELCOME! <?php echo $uname; ?>

另一方面,在第一个文件(loginscript.php)中,您应该在<?php之前添加session_start(),因为您有ini_set('display_errors', true);,这将中断您的会话。

使用;

WELCOME! <?php echo $uname; ?>