session问题!永远的痛呀!
tq85 2004-04-01 11:51:58 看前面一个帖子的时候,有位大虾给了个英文!
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
register_globals: important note:
Since PHP 4.2.0, the default value for the PHP directive register_globals is off. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.
Caution:
This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.
Caution:
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
看的眼晕,好象大意是说!新版本里面如果使用$_session建议不要使用
session_register(), session_is_registered(), and session_unregister().
中间那段是说如果global给disable的话这三个函数会出现问题的!
底下一个大虾讲的更明白!
"
应该在新版的php已经没必要用session_register了
直接对$_SESSION["foo"]进行赋值/取值就可以了,和asp中一样。
"
如获至宝!正好我写的登陆程序始终有问题!
写了一个测试
first.php
<form action="login.php" method="post">
<p>用户名:
<input name="username" type="text" id="username">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
login.php
<?php
session_start();
$_SESSION['username']=$_POST['username'];
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<?php echo $_SESSION['username'];?>
</body>
</html>
2.php
<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<?php echo $_SESSION['username'];?>
</body>
</html>
这个工作的很好,session实现的传递!
可是正式又写一个的时候出问题了!
login.php
<?php
session_start();
?>
<?php require("userconfig.php");?>
<?php
if(empty($_POST['username'])||empty($_POST['password'])||empty($_POST['usertype'])) echo "error";
else
{
if($_POST['usertype']==0) $file=$companyfile;
else $file=$studentfile;
$userfile=$datapath.$file;
//echo $userfile;
$arr_row=file($data_file);
if($arr_row)
{
$num=count($arr_row);
for($i=0;$i<$num;$i++)
{
list($username,$password,$email)=explode("|",$arr_row[$i]);
if($username==$_POST['username']&&$password==$_POST['password'])
{
$_SESSION['user_name']=$username;
//$loginsatus=1;
break;
}
}
if(!empty($_SESSION['user_name']))
{
echo $_SESSION['user_name'];
}
else
{
echo "没有登陆成功";
}
}
}
?>
我一登陆就出现空白页!
而且我还可以在定义session之前echo
我前面注释掉的那个
//echo $userfile;
可以输出不报错!不清楚怎么回事,请大虾帮助!
我前面试过用那种传统的
session_register($username);
可也是出现空白页的!