【发布时间】:2018-05-25 04:13:43
【问题描述】:
我的 PHP 表单有问题。我的表单上有一个图像作为提交按钮,并且在提交表单时我似乎无法设置会话令牌。代码的编写方式是在页面加载时设置令牌。这并没有给我带来太多困扰,但我需要在提交表单时设置或重置它。谁能告诉我我做错了什么?代码如下:
<?php
// Initiate the session.
session_start();
// Simple function to return a timestamp.
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
// Generate the token.
function generateToken() {
// generate a token from a unique value, took from microtime...
$token = "myFormToken-" . microtime_float();
// Write the generated token to the session variable to check it against the hidden field when the form is sent
$_SESSION['myFormToken'] = $token;
return $token;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>This Is My Webpage...</title>
</head>
<body>
<h1>Click on the image below to be taken to the next page..</h1>
<br /><br />
<!-- BEGIN My Form -->
<form action="http://www.mywebsite.com/mypage.php" method="post" target="_top">
<input type="hidden" name="myFormToken" value="<?php echo generateToken(); ?>">
<input type="image" src="http://www.mywebsite.com/myimage.jpg" border="0" name="submit" alt="Click this image!">
</form>
<!-- END My Form -->
</body>
</html>
感谢您的帮助!
布赖恩
【问题讨论】:
-
你应该检查表单是否提交。