我马上就要考试了。我被课程卡住了。在大多数情况下,无论我尝试过什么,它对我都不起作用。它曾经对我有效,但刷新页面后,我再也看不到它有效。我的样品:
qwer.php:
<?php
session_name('Private');
session_id('TEST');
if(isset($_POST["Submit"])){
session_start();
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
<input type="text" name="imie"><br>
<input type="submit" name="Submit">
</form>
<a href='qwew.php'>Link to protected file</a>
</body>
</html>
<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>
qwew.php
<?php
session_name('Private');
session_id('TEST');
session_start();
echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
session_destroy();
?>
问题是,表单和重定向到另一个页面应该通过点击提交来完成。请帮我弄清楚我哪里错了。此外,我忘了标注我正在使用XAMPP,session.use_cookies和session.use_trans_sid已启用
qwer.php
<?php
session_start();
if(isset($_POST["Submit"])) {
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
header('Location:qwew.php');
die();
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form name="myform" method="post" action="qwer.php" onsubmit="return validateform();">
<input type="text" name="imie"><br>
<input type="submit" name="Submit">
</form>
<a href='qwew.php'>Link to protected file</a>
</body>
</html>
qwew.php
session_start();
if(isset($_SESSION["lolvalue"]) && isset($_SESSION["counter"]))
echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
else {
header('Location:qwer.php');
die();
}
此代码永远不会执行:
if(isset($_POST["Submit"])){
session_start();
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
}
这是因为您的HTML表单正在向qwew.php
文件发送POST
请求,而不是向qwer.php
发送。
qwew.php
<?php
session_start();
if(isset($_POST["Submit"])){
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
<input type="text" name="imie"><br>
<input type="submit" name="Submit">
</form>
<a href='qwew.php'>Link to protected file</a>
</body>
</html>
<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>