下一篇 » « 上一篇

Figure 16-6: Checking Session ID

作者:semirock    时间:2009-06-25    来源:http://444p.com    点击:4153    本文共1篇文章 字体:[ ]

Figure 16-6: Checking Session ID

<HTML>
<HEAD>
<TITLE>Figure 16-6</TITLE>
</HEAD>
<BODY>
<?
/*
** Demonstration of using session identifiers
*/

// generates a session id
function SessionID($length=8)
{
/*
** Set pool of possible characters
*/
$Pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$Pool .= "abcdefghijklmnopqrstuvwxyz";

for($index = 0; $index < $length; $index++)
{
$sid .= substr($Pool,
(rand()%(strlen($Pool))), 1);
}

return($sid);
}

// Seed the generator
srand(time());


//start HTML page
print("<HTML>\n");
print("<HEAD>\n");
print("<TITLE>Figure 16-6</TITLE>\n");
print("</HEAD>\n");

print("<BODY>\n"); php学习之家http://www.444p.com

//set up connection to database
$mysql_link = mysql_connect("localhost", "httpd", "");
mysql_select_db("test", $mysql_link);

//clear out any old sessions
$Query = "DELETE FROM session ";
$Query .= "WHERE LastAction < '";
$Query .= date("Y-m-d H:i:s", (time()-10800));
$Query .= "'";
mysql_query($Query, $mysql_link);

//check session
if(isset($session))
{
//we have a session, so check it
$Query = "SELECT * ";
$Query .= "FROM session ";
$Query .= "WHERE ID='$session' ";

$mysql_result = mysql_query($Query, $mysql_link);

if(mysql_numrows($mysql_result))
{
//session exists, update last action
$Query = "UPDATE session ";
$Query .= "SET LastAction = now() ";
$Query .= "WHERE ID='$session' "; www.444p.com php学习之家
mysql_query($Query, $mysql_link);
}
else
{
//session is bad
print("Bad Session ID ($session)!<BR>\n");
$session = "";
}
}

//if session is empty, we need to create it
if($session == "")
{
//no session, so create one
$session = SessionID(8);

//insert session to database
$Query = "INSERT INTO session ";
$Query .= "VALUES ('$session', now()) ";
mysql_query($Query, $mysql_link);
}

print("You session identifier is $session<BR>\n");
print("<A HREF=\"16-6.php3?session=$session\">");
print("Refresh Page");
print("</A><BR>\n");

//close page
print("</BODY>\n");
print("</HTML>\n");
?>
</BODY> www.444p.com
</HTML>
责任编辑:semirock
发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


热门搜索: [html][html][else][MYsql][mysql][调用][date][print][配置][配置]

《PHP函数》点击排行