这本来是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 >
< 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>
< 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>
< 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>