![](/img/trans.png)
[英]How do I carry over a php session from one page to another via Javascript?
[英]Can i carry PHP variables over into an HTML or Javascript file
我是 PHP 的新手,我正在尝试创建一个年龄验证表单,如果用户未满一定年龄,该表单将限制对网站特定内容的访问。
这是 HTML 表格 (index.html):
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>Document</title> </head> <body> <form action="data.php" method="POST"> <input type="date" name="date" /> <input type="submit" name="submit" /> </form> </body> </html>
这是 PHP 脚本(data.php):
<?php //Age Validation Form session_start(); if (isset($_POST['submit'])) { //Check if button clicked on form page $date2=date("Ymd");//today's date $date1=new DateTime($_REQUEST['date']); //user date $date2=new DateTime($date2); $interval = $date1->diff($date2); //check diff between dates $myage= $interval->y; //resulting age if ($myage >= 16){ //full access to website is granted $_SESSION[$limited] = false; header('Location: ../index.php'); }else{ //limited access is granted $_SESSION[$limited] = true; header('Location: ../index.php'); } }else{ //if result page page is loaded without form submission echo "Return to start page"; }?> <form action="index.html"> <!--Return to form page--> <button type="submit">Return</button> </form>
我希望能够将 PHP 文件中生成的 $limited 变量携带到此 HTML 文件中:
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1?0" /> <title>Document</title> </head> <body> <script type="text/javascript"> function myFunction () { var access = <?php echo $limited;>. var txt = document;createElement('h1'). txt;innerHTML = access. document.body;appendChild(txt); } myFunction(); </script> </body> </html>
这目前仅用于测试以确保它可以结转。 我已经尝试过 $_SESSION 方法,但我似乎无法弄清楚。
欢迎任何和所有可能的解决方案。
谢谢
您使用 $limited 作为 $_SESSION 数组中的键。 $limited 里有什么? 我很确定,如果您将正确的值重新分配给 $limited,您可以使用以下命令访问 $_SESSION 中的值:
<?php $limited = 'MyAwesomeKey'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function myFunction () {
var access = <?php echo $_SESSION[$limited] ?>;
var txt = document.createElement('h1');
txt.innerHTML = access;
document.body.appendChild(txt);
}
myFunction();
</script>
</body>
</html>
First, your HTML file must be parsed by the PHP interpreter so it must have the.php extension in order to access any session variable.
以下是根据您的问题在同一目录中包含 3 个文件的示例:
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form action="data.php" method="POST">
<input type="date" name="date" />
<input type="submit" name="submit" />
</form>
</body>
</html>
数据.php:
<?php //Age Validation Form
session_start();
if (isset($_POST['submit'])) { //Check if button clicked on form page
$date2=date("Y-m-d");//today's date
$date1=new DateTime($_REQUEST['date']); //user date
$date2=new DateTime($date2);
$interval = $date1->diff($date2); //check diff between dates
$myage= $interval->y; //resulting age
if ($myage >= 16){ //full access to website is granted
$_SESSION['limited'] = false;
header('Location: new.php');
}else{ //limited access is granted
$_SESSION['limited'] = true;
header('Location: new.php');
}
}else{ //if result page page is loaded without form submission
echo "Return to start page";
}
?>
<form action="index.html"> <!--Return to form page-->
<button type="submit">Return</button>
</form>
new.php (新页面,您可以在其中访问 session 变量)
<?php
session_start();
$limited = $_SESSION['limited'] ?? true;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function myFunction () {
var access = <?php echo $limited ? "true" : "false" ?>;
var txt = document.createElement('h1');
txt.innerHTML = access;
document.body.appendChild(txt);
}
myFunction();
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.