PHP的一个关于SESSION的小白问题 sessionphpfunction 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 跟session无关你检查一下下面这个方法$code->useCode(); 错误信息很明显,执行完$_SESSION['code']=$code->useCode(); 后$code对象不存在了! useCode返回的是一个啥子?不会是一张图片吧? 如果你贴出来的代码没有删减,我有2个假设~1,$code->useCode()运行后$code被修改或销毁。但被调用的类方法如何销毁调用对象?不知道能不能~2,useCode()方法中调用了outCode()方法,但调用它的变量不是合法对象。 1,$code->useCode()运行后$code被修改或销毁!!! 这才是根本原因。在useCode()中有问题。 大家说的都不对,这个段代码我研究了一个整天了,从上午一直坐到现在。原来是优先级的问题$_session['code']=$code->useCode();把整个$Code对象赋值给了SESSION。这真的很不可思议。明明赋值符在任何语言中都是低优先级的在此却出现了例外。这个类的功能是产生一个验证码,从调用它时产生前后数据不一致到这个SESSION的问题。我一直在努力地,孤独地一个个解决。其中的滋味和感想。我想大家都一定感受过。能得到大家的回答真的很开心,证明了我不是一个人在战斗! 路漫漫其修远兮,吾将上下而求索。刚刚接触PHP不久的我与大家共勉!共进! 想到过优先级的问题,回复之前特意写了个简单类测试,没毛病啊,而且我经常$data = $DB->select();这样难道只有类似$_session的全局变量有问题?还是出在[]这个数组索引上?能不能把code_class.php代码贴一下呢?如果需要保密就不要贴了,但这个问题确定是优先级? 搞笑——!session_start();class a{ function b(){ return 'asd'; } function c() { echo '123'; }}$a=new a;$_SESSION['a'] = $a->b();$a->c(); //123echo $_SESSION['a']; //asd明明是你useCode();方法有问题! 想到过优先级的问题,回复之前特意写了个简单类测试,没毛病啊,而且我经常$data = $DB->select();这样难道只有类似$_session的全局变量有问题?还是出在[]这个数组索引上?能不能把code_class.php代码贴一下呢?如果需要保密就不要贴了,但这个问题确定是优先级? 这个是类的代码<?php class Code{ private $width; private $high; var $code=''; private $im; private $while; private $red; private $green; private $blue; private $black; private $bgcolor; private $codecolor; private $num; function __construct($w,$h,$n){ $this->width=$w; //初始化宽度 $this->high=$h; //初始化高度 $this->num=$n; //初始化验证码的个数 $this->im=imagecreate($w,$h); //生成一个画板 $this->setColor(); //该画板能使用的颜色 $this->setBcolor(); //设置画板的背景颜色 $this->getCode(); //获得验证码的字符串 // $this->creCode(); //创建验证码的图片 // $this->desturb(); //$this->saveImage(); //保存该图片,并显示 } function setColor(){ $this->while=imagecolorallocate($this->im,255,255,255); $this->red=imagecolorallocate($this->im,255,0,0); $this->green=imagecolorallocate($this->im,0,255,0); $this->blue=imagecolorallocate($this->im,0,0,255); $this->black=imagecolorallocate($this->im,0,0,0); $this->bgcolor=imagecolorallocate($this->im,rand(200,255),rand(200,255),rand(200,255)); $this->codecolor=imagecolorallocate($this->im,rand(0,122),rand(0,122),rand(0,122)); } function setBcolor(){ imagefill($this->im,0,0,$this->bgcolor) or die("设置背景颜色失败"); } function getCode(){ $s="abcdefg1234567ABCDEFG你我他"; $str=iconv("utf-8","utf-8",$s); $s=""; for($i=0;$i<($this->num);$i++){ $s.=$str{rand(0,strlen($str)-1)}; } $this->code=$s; } function useCode(){ return $this->code; } function creCode(){ //imagettftext($this->im,20,10,$this->width/7,$this->high,$this->blue,"C:/Windows/Fonts/simsun.ttc",$this->code); imagerectangle($this->im,0,0,$this->width-1,$this->high-1,$this->black); $num=strlen($this->code); for($i=0;$i<$num;$i++){ $fontsize=rand(3,5); $x=3+($this->width/$num)*$i; $fh=imagefontheight($this->code{$i}); $y=$this->high/2-$fh/2; imagechar($this->im,$fontsize,$x,$y,$this->code{$i},$this->codecolor); } } function desturb(){ /* $style = array($this->blue,$this->white); imagesetstyle($this->im, $style); for($i=1;$i<11;$i++) { $jg=3*$i; imageline($this->im,0,$jg,$this->width,$jg,IMG_COLOR_STYLED); } */ for($i;$i<100;$i++) { imagesetpixel($this->im,rand(1,$this->width),rand(1,$this->high),$this->codecolor); } for($i=0;$i<20;$i++) { imagearc($this->im,rand(-10,$this->width+10),rand(-10,$this->high+10),rand(30,300),rand(30,300),0,90,$this->codecolor); } } function outCode(){ header('Content-type:image/png'); $this->creCode(); $this->desturb(); imagepng($this->im); } function destCode(){ imagedestroy($this->im); } }?> 这个是调用的PHP页面<?php session_start(); include "code_class.php"; $code=new Code(200,30,4); $_SESSION['co']=($code->useCode()); //echo "执行前SESSION的值:".$_SESSION['co']."<br>"; $code->outCode(); //echo "执行前后SESSION的值:".$_SESSION['co']."<br>"; $code->destCode();?>这个是测试表单<?php session_start(); echo "--".strtoupper($_SESSION['co']); echo "<br>"; echo "--".strtoupper($_POST['code']); if(isset($_POST['code'])){ if(strtoupper($_SESSION['co'])==strtoupper($_POST['code'])){ echo '验证成功'; } else echo '验证失败'; } ?><html> <head> <title>我的第一个验证码</title> <script> function myF(){ document.alert("你好"); } </script> </head> <body> <form action='gd.php' method='post'> <div>用户名:<input type='text' name='user'></div> <div>密  码: <input type='password' name='ps'></div> <div>请输入验证码:<input type='text' name='code' size='12' onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> </div> <img name='image'src="demo.php" onclick="this.src='usecode.php?'+Math.random()"> <a>看不清点图片</a> <div>    <input type='submit' value="确认">      <input type='reset' value="重设" </div> </form> </body></html>欢迎提供宝贵建议与意见。只要有帮助也欢迎来喷 搞笑——!session_start();class a{ function b(){ return 'asd'; } function c() { echo '123'; }}$a=new a;$_SESSION['a'] = $a->b();$a->c(); //123echo $_SESSION['a']; //asd明明是你useCode();方法有问题!这个是类的代码<?php class Code{ private $width; private $high; var $code=''; private $im; private $while; private $red; private $green; private $blue; private $black; private $bgcolor; private $codecolor; private $num; function __construct($w,$h,$n){ $this->width=$w; //初始化宽度 $this->high=$h; //初始化高度 $this->num=$n; //初始化验证码的个数 $this->im=imagecreate($w,$h); //生成一个画板 $this->setColor(); //该画板能使用的颜色 $this->setBcolor(); //设置画板的背景颜色 $this->getCode(); //获得验证码的字符串 // $this->creCode(); //创建验证码的图片 // $this->desturb(); //$this->saveImage(); //保存该图片,并显示 } function setColor(){ $this->while=imagecolorallocate($this->im,255,255,255); $this->red=imagecolorallocate($this->im,255,0,0); $this->green=imagecolorallocate($this->im,0,255,0); $this->blue=imagecolorallocate($this->im,0,0,255); $this->black=imagecolorallocate($this->im,0,0,0); $this->bgcolor=imagecolorallocate($this->im,rand(200,255),rand(200,255),rand(200,255)); $this->codecolor=imagecolorallocate($this->im,rand(0,122),rand(0,122),rand(0,122)); } function setBcolor(){ imagefill($this->im,0,0,$this->bgcolor) or die("设置背景颜色失败"); } function getCode(){ $s="abcdefg1234567ABCDEFG你我他"; $str=iconv("utf-8","utf-8",$s); $s=""; for($i=0;$i<($this->num);$i++){ $s.=$str{rand(0,strlen($str)-1)}; } $this->code=$s; } function useCode(){ return $this->code; } function creCode(){ //imagettftext($this->im,20,10,$this->width/7,$this->high,$this->blue,"C:/Windows/Fonts/simsun.ttc",$this->code); imagerectangle($this->im,0,0,$this->width-1,$this->high-1,$this->black); $num=strlen($this->code); for($i=0;$i<$num;$i++){ $fontsize=rand(3,5); $x=3+($this->width/$num)*$i; $fh=imagefontheight($this->code{$i}); $y=$this->high/2-$fh/2; imagechar($this->im,$fontsize,$x,$y,$this->code{$i},$this->codecolor); } } function desturb(){ /* $style = array($this->blue,$this->white); imagesetstyle($this->im, $style); for($i=1;$i<11;$i++) { $jg=3*$i; imageline($this->im,0,$jg,$this->width,$jg,IMG_COLOR_STYLED); } */ for($i;$i<100;$i++) { imagesetpixel($this->im,rand(1,$this->width),rand(1,$this->high),$this->codecolor); } for($i=0;$i<20;$i++) { imagearc($this->im,rand(-10,$this->width+10),rand(-10,$this->high+10),rand(30,300),rand(30,300),0,90,$this->codecolor); } } function outCode(){ header('Content-type:image/png'); $this->creCode(); $this->desturb(); imagepng($this->im); } function destCode(){ imagedestroy($this->im); } }?> 这个是调用的PHP页面<?php session_start(); include "code_class.php"; $code=new Code(200,30,4); $_SESSION['co']=($code->useCode()); //echo "执行前SESSION的值:".$_SESSION['co']."<br>"; $code->outCode(); //echo "执行前后SESSION的值:".$_SESSION['co']."<br>"; $code->destCode();?>这个是测试表单<?php session_start(); echo "--".strtoupper($_SESSION['co']); echo "<br>"; echo "--".strtoupper($_POST['code']); if(isset($_POST['code'])){ if(strtoupper($_SESSION['co'])==strtoupper($_POST['code'])){ echo '验证成功'; } else echo '验证失败'; } ?><html> <head> <title>我的第一个验证码</title> <script> function myF(){ document.alert("你好"); } </script> </head> <body> <form action='gd.php' method='post'> <div>用户名:<input type='text' name='user'></div> <div>密  码: <input type='password' name='ps'></div> <div>请输入验证码:<input type='text' name='code' size='12' onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> </div> <img name='image'src="demo.php" onclick="this.src='usecode.php?'+Math.random()"> <a>看不清点图片</a> <div>    <input type='submit' value="确认">      <input type='reset' value="重设" </div> </form> </body></html>欢迎提供宝贵建议与意见。只要有帮助也欢迎来喷还有我这个验证码不能输出中文,一输出就乱码。 下的fastcgi x86的无法安装 用php把csv文件转换成sql导入到数据库要怎么做? 留言板里如何添加评论功能 購物車的奇怪問題,在線等, 急急急! 如何将C/C++语言写的算法编译成PHP可用的dll模块,PHP如何调用?请高人指点,请给个例子 一道基础php面试题,想不通 一个下划线加两个刮符是什么意思? 剖析网页字符集的设置顺序 求助:各位大虾,帮我看看这个流控制里的值为什么就不是“1”呢? 为什么使用了模板session就提出了warning? 刚刚从.net转到无线后端开发,面对各种需要学习的。。。。 新手问个正则问题。。。
你检查一下下面这个方法$code->useCode(); 错误信息很明显,执行完$_SESSION['code']=$code->useCode(); 后$code对象不存在了!
1,$code->useCode()运行后$code被修改或销毁。但被调用的类方法如何销毁调用对象?不知道能不能~
2,useCode()方法中调用了outCode()方法,但调用它的变量不是合法对象。
这才是根本原因。在useCode()中有问题。
想到过优先级的问题,回复之前特意写了个简单类测试,没毛病啊,而且我经常$data = $DB->select();这样
难道只有类似$_session的全局变量有问题?还是出在[]这个数组索引上?
能不能把code_class.php代码贴一下呢?如果需要保密就不要贴了,但这个问题确定是优先级?
搞笑——!session_start();
class a{
function b(){
return 'asd';
}
function c() {
echo '123';
}
}
$a=new a;
$_SESSION['a'] = $a->b();
$a->c(); //123
echo $_SESSION['a']; //asd明明是你useCode();方法有问题!
想到过优先级的问题,回复之前特意写了个简单类测试,没毛病啊,而且我经常$data = $DB->select();这样
难道只有类似$_session的全局变量有问题?还是出在[]这个数组索引上?
能不能把code_class.php代码贴一下呢?如果需要保密就不要贴了,但这个问题确定是优先级? 这个是类的代码
<?php
class Code{
private $width;
private $high;
var $code='';
private $im;
private $while;
private $red;
private $green;
private $blue;
private $black;
private $bgcolor;
private $codecolor;
private $num;
function __construct($w,$h,$n){
$this->width=$w; //初始化宽度
$this->high=$h; //初始化高度
$this->num=$n; //初始化验证码的个数
$this->im=imagecreate($w,$h); //生成一个画板
$this->setColor(); //该画板能使用的颜色
$this->setBcolor(); //设置画板的背景颜色
$this->getCode(); //获得验证码的字符串
// $this->creCode(); //创建验证码的图片
// $this->desturb();
//$this->saveImage(); //保存该图片,并显示
}
function setColor(){
$this->while=imagecolorallocate($this->im,255,255,255);
$this->red=imagecolorallocate($this->im,255,0,0);
$this->green=imagecolorallocate($this->im,0,255,0);
$this->blue=imagecolorallocate($this->im,0,0,255);
$this->black=imagecolorallocate($this->im,0,0,0);
$this->bgcolor=imagecolorallocate($this->im,rand(200,255),rand(200,255),rand(200,255));
$this->codecolor=imagecolorallocate($this->im,rand(0,122),rand(0,122),rand(0,122));
}
function setBcolor(){
imagefill($this->im,0,0,$this->bgcolor) or die("设置背景颜色失败");
}
function getCode(){
$s="abcdefg1234567ABCDEFG你我他";
$str=iconv("utf-8","utf-8",$s);
$s="";
for($i=0;$i<($this->num);$i++){
$s.=$str{rand(0,strlen($str)-1)};
}
$this->code=$s;
}
function useCode(){
return $this->code;
}
function creCode(){
//imagettftext($this->im,20,10,$this->width/7,$this->high,$this->blue,"C:/Windows/Fonts/simsun.ttc",$this->code);
imagerectangle($this->im,0,0,$this->width-1,$this->high-1,$this->black);
$num=strlen($this->code);
for($i=0;$i<$num;$i++){
$fontsize=rand(3,5);
$x=3+($this->width/$num)*$i;
$fh=imagefontheight($this->code{$i});
$y=$this->high/2-$fh/2;
imagechar($this->im,$fontsize,$x,$y,$this->code{$i},$this->codecolor);
} }
function desturb(){
/* $style = array($this->blue,$this->white);
imagesetstyle($this->im, $style);
for($i=1;$i<11;$i++)
{ $jg=3*$i;
imageline($this->im,0,$jg,$this->width,$jg,IMG_COLOR_STYLED);
}
*/
for($i;$i<100;$i++)
{
imagesetpixel($this->im,rand(1,$this->width),rand(1,$this->high),$this->codecolor);
}
for($i=0;$i<20;$i++)
{
imagearc($this->im,rand(-10,$this->width+10),rand(-10,$this->high+10),rand(30,300),rand(30,300),0,90,$this->codecolor);
}
}
function outCode(){
header('Content-type:image/png');
$this->creCode();
$this->desturb();
imagepng($this->im);
}
function destCode(){
imagedestroy($this->im);
}
}
?>
这个是调用的PHP页面
<?php
session_start();
include "code_class.php";
$code=new Code(200,30,4); $_SESSION['co']=($code->useCode());
//echo "执行前SESSION的值:".$_SESSION['co']."<br>";
$code->outCode();
//echo "执行前后SESSION的值:".$_SESSION['co']."<br>";
$code->destCode();?>
这个是测试表单
<?php
session_start();
echo "--".strtoupper($_SESSION['co']); echo "<br>";
echo "--".strtoupper($_POST['code']);
if(isset($_POST['code'])){
if(strtoupper($_SESSION['co'])==strtoupper($_POST['code'])){
echo '验证成功';
}
else echo '验证失败';
}
?><html>
<head>
<title>我的第一个验证码</title>
<script>
function myF(){
document.alert("你好");
}
</script>
</head>
<body>
<form action='gd.php' method='post'>
<div>用户名:<input type='text' name='user'></div>
<div>密  码: <input type='password' name='ps'></div>
<div>请输入验证码:<input type='text' name='code' size='12'
onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> </div>
<img name='image'src="demo.php" onclick="this.src='usecode.php?'+Math.random()"> <a>看不清点图片</a>
<div>    <input type='submit' value="确认">      <input type='reset' value="重设" </div>
</form>
</body>
</html>欢迎提供宝贵建议与意见。只要有帮助也欢迎来喷
搞笑——!session_start();
class a{
function b(){
return 'asd';
}
function c() {
echo '123';
}
}
$a=new a;
$_SESSION['a'] = $a->b();
$a->c(); //123
echo $_SESSION['a']; //asd明明是你useCode();方法有问题!这个是类的代码
<?php
class Code{
private $width;
private $high;
var $code='';
private $im;
private $while;
private $red;
private $green;
private $blue;
private $black;
private $bgcolor;
private $codecolor;
private $num;
function __construct($w,$h,$n){
$this->width=$w; //初始化宽度
$this->high=$h; //初始化高度
$this->num=$n; //初始化验证码的个数
$this->im=imagecreate($w,$h); //生成一个画板
$this->setColor(); //该画板能使用的颜色
$this->setBcolor(); //设置画板的背景颜色
$this->getCode(); //获得验证码的字符串
// $this->creCode(); //创建验证码的图片
// $this->desturb();
//$this->saveImage(); //保存该图片,并显示
}
function setColor(){
$this->while=imagecolorallocate($this->im,255,255,255);
$this->red=imagecolorallocate($this->im,255,0,0);
$this->green=imagecolorallocate($this->im,0,255,0);
$this->blue=imagecolorallocate($this->im,0,0,255);
$this->black=imagecolorallocate($this->im,0,0,0);
$this->bgcolor=imagecolorallocate($this->im,rand(200,255),rand(200,255),rand(200,255));
$this->codecolor=imagecolorallocate($this->im,rand(0,122),rand(0,122),rand(0,122));
}
function setBcolor(){
imagefill($this->im,0,0,$this->bgcolor) or die("设置背景颜色失败");
}
function getCode(){
$s="abcdefg1234567ABCDEFG你我他";
$str=iconv("utf-8","utf-8",$s);
$s="";
for($i=0;$i<($this->num);$i++){
$s.=$str{rand(0,strlen($str)-1)};
}
$this->code=$s;
}
function useCode(){
return $this->code;
}
function creCode(){
//imagettftext($this->im,20,10,$this->width/7,$this->high,$this->blue,"C:/Windows/Fonts/simsun.ttc",$this->code);
imagerectangle($this->im,0,0,$this->width-1,$this->high-1,$this->black);
$num=strlen($this->code);
for($i=0;$i<$num;$i++){
$fontsize=rand(3,5);
$x=3+($this->width/$num)*$i;
$fh=imagefontheight($this->code{$i});
$y=$this->high/2-$fh/2;
imagechar($this->im,$fontsize,$x,$y,$this->code{$i},$this->codecolor);
} }
function desturb(){
/* $style = array($this->blue,$this->white);
imagesetstyle($this->im, $style);
for($i=1;$i<11;$i++)
{ $jg=3*$i;
imageline($this->im,0,$jg,$this->width,$jg,IMG_COLOR_STYLED);
}
*/
for($i;$i<100;$i++)
{
imagesetpixel($this->im,rand(1,$this->width),rand(1,$this->high),$this->codecolor);
}
for($i=0;$i<20;$i++)
{
imagearc($this->im,rand(-10,$this->width+10),rand(-10,$this->high+10),rand(30,300),rand(30,300),0,90,$this->codecolor);
}
}
function outCode(){
header('Content-type:image/png');
$this->creCode();
$this->desturb();
imagepng($this->im);
}
function destCode(){
imagedestroy($this->im);
}
}
?>
这个是调用的PHP页面
<?php
session_start();
include "code_class.php";
$code=new Code(200,30,4); $_SESSION['co']=($code->useCode());
//echo "执行前SESSION的值:".$_SESSION['co']."<br>";
$code->outCode();
//echo "执行前后SESSION的值:".$_SESSION['co']."<br>";
$code->destCode();?>
这个是测试表单
<?php
session_start();
echo "--".strtoupper($_SESSION['co']); echo "<br>";
echo "--".strtoupper($_POST['code']);
if(isset($_POST['code'])){
if(strtoupper($_SESSION['co'])==strtoupper($_POST['code'])){
echo '验证成功';
}
else echo '验证失败';
}
?><html>
<head>
<title>我的第一个验证码</title>
<script>
function myF(){
document.alert("你好");
}
</script>
</head>
<body>
<form action='gd.php' method='post'>
<div>用户名:<input type='text' name='user'></div>
<div>密  码: <input type='password' name='ps'></div>
<div>请输入验证码:<input type='text' name='code' size='12'
onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()"> </div>
<img name='image'src="demo.php" onclick="this.src='usecode.php?'+Math.random()"> <a>看不清点图片</a>
<div>    <input type='submit' value="确认">      <input type='reset' value="重设" </div>
</form>
</body>
</html>欢迎提供宝贵建议与意见。只要有帮助也欢迎来喷还有我这个验证码不能输出中文,一输出就乱码。