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);
可也是出现空白页的!
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ice_berg16 2004-04-02
  • 打赏
  • 举报
回复
而且我还可以在定义session之前echo
我前面注释掉的那个
//echo $userfile;

只要不在session_start()前面有输出就行了。这个是可以的

出现空白页的原因可能是:
1. php.ini中display_errors = off 不显示错误,改成on
2。$_SESSION['username']值存在,没有显示出来(可能是空格)
tq85 2004-04-02
  • 打赏
  • 举报
回复
谢谢大虾回答
虽然无关这个问题,可是学到很多方法!
原来是这句错了
$arr_row=file($data_file);

应该是
$arr_row=file($userfile);

在此感谢大虾的帮助!
tq85 2004-04-02
  • 打赏
  • 举报
回复
1. php.ini中display_errors = off 不显示错误,改成on
我的能显示错误的,dispaly_errors=on

2。$_SESSION['username']值存在,没有显示出来(可能是空格)

没有,并不是空格,我查看过源代码的!

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧