Home > php教程 > PHP源码 > body text

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

WBOY
Release: 2016-06-08 17:31:25
Original
954 people have browsed it
<script>ec(2);</script>

这本来是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()."">数据库测试
    

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template