PHP中使session可以跨窗口运行的方法

这本来是session的基本用法,但在php中就不灵了。不过我们可以把session变量注册成"跨窗口的全局变量"。但这有一个条件,就是要向需要使用该session变量的窗口发送变量名为session_name(),值为session_id()的变量,用表单或者在url后面用?带上都可以.并且在使用session变量的页面的一开始处调用session_start()。

例子如下:

login.php文件:

< html >
< head >
< meta http - equiv = " Content-Type "  content = " text/html; charset=utf-8 "   />
< title > 登陆画面 </ title >
</ head >
< body >
< form action = " result.php "  method = " post " >
  
< table width = " 100% "  border = " 0 " >
  
< tr >
    
< td align = " center "  valign = " middle " >< p > 测试系统 ----- 登陆画面 </ p >
        
< table width = " 250 "  style = " border-collapse:collapse; border-color:#000000 "
            border
= " 1 "  cellpadding = " 2 "  cellspacing = " 2 " >
            
< tr >
                
< td width = " 30% " > 用户 :</ td >
                
< td >< input name = " username "  type = " text "  style = " width:150px " /></ td >
            
</ tr >
            
< tr >
                
< td > 密码 :</ td >
                
< td >< input name = " password "  type = " password "  style = " width:150px " /></ td >
            
</ tr >
            
< tr >
                
< td colspan = " 2 "  align = " center " >< input type = " submit "  value = " 登陆 " /></ td >
            
</ tr >
        
</ table >
    
</ td >
  
</ tr >
</ table >
</ form >
</ body >
</ html >

result.php文件:

< html >
< head >
< meta http - equiv = " Content-Type "  content = " text/html; charset=utf-8 "   />
< title > 结果画面 </ title >
</ head >
< body >
< form method = " post " >
    欢迎你, 
    
<? php
        @
session_start ();
        
if ( isset ( $_POST [ " username " ])){
            
$username   =   htmlspecialchars ( $_POST [ " username " ]);
            
$_SESSION [ ' username ' =   $username ;
        } 
else  {
            
$username   =   $_SESSION [ ' username ' ];
        }
        
echo   $username
    
?>
    
< br />
    
< br />
    
< a href = " login.php " > 返回 </ a >
    
< a href = " database.php? " . session_name () . " = " . session_id () . " ">数据库测试</a>
    
</form>
</body>
</html>

database.php文件:

< html >
< head >
< meta http - equiv = " Content-Type "  content = " text/html; charset=utf-8 "   />
< title > 结果画面 </ title >
</ head >
< body >
< form method = " post " >
    Email:
    
<? php
        @
session_start ();
    
        
$conn   =  mssql_pconnect( ' localhost ' ,   ' sa ' ,   ' sa ' );
        mssql_select_db(
' netstore ' );
        
$query   =   mssql_query( ' select * from CUSTOMER ' ,   $conn );
        
$name   =   mssql_result( $query ,   0 ,   ' email ' );
        
echo   $name ;
    
?>
    
< br />
    
< br />
    
< a href = " result.php? " . session_name () . " = " . session_id () . " ">返回</a>
</form>
</body>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值