[英]Can i set session variables when posting data via javascript?
I created a simple PHP login authentication script, now i want to override the posting of data from the form to the auth script so that it does it via ajax. 我创建了一个简单的PHP登录身份验证脚本,现在我想覆盖从表单到auth脚本的数据发布,以便它通过ajax进行。
I made it so that the PHP returns JSON data in the format {"success":[1 OR 0],"error";"ANY ERROR MESSAGE RELATED TO SUCCESS = 0"}
, this works perfectly and when i try and use the login form it returns success:1
when i serialize the form and post it to the auth script with a correct username+password and it returns success:0
and the reason why with an incorrect login, on the original method where the form posted to the auth script, then the auth script redirected to a protected page after succesful login i have replaced this with the javascript changing the window location after it recieves a succesful login, only problem being is that is seems the session variables havent been set from posting to the auth script via this method. 我这样做是为了让PHP以
{"success":[1 OR 0],"error";"ANY ERROR MESSAGE RELATED TO SUCCESS = 0"}
的格式返回JSON数据,当我尝试使用登录表单,返回success:1
,当我序列化的形式,并将其与正确的用户名+密码邮寄到身份验证脚本,它返回success:0
,为什么用不正确的登录,在原来的方法,其中的形式发布到的原因auth脚本,然后在成功登录后将auth脚本重定向到受保护的页面,我在成功接收登录后用javascript更改了窗口位置,将其替换为javascript,唯一的问题是似乎会话变量尚未设置为从发布到通过此方法验证脚本。
Is there a way i can make the session variables stick without actually having to direct to the page? 有没有一种方法可以使会话变量保持不变而无需直接定向到页面?
Creating Session Variable using JavaScript 使用JavaScript创建会话变量
<?php session_start(); ?>
<html>
<head>
<script type='text/javascript'>
function setSession(variable, value) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "setSession.php?variable=" + variable + "&value=" + value, true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
if(isset($_SESSION['login']) && $_SESSION['login'] == "true")
echo "Session Active. <a href=\"javascript:setSession('login', 'false')\"><input type='submit' value='De-Activate'></a>";
else
echo "Session Inactive. <a href=\"javascript:setSession('login', 'true')\"><input type='submit' value='Activate'></a>";
echo "<a href=\"index.php\"><input type='submit' value='Re-Load Page'></a>";
?>
</body>
</html>
Assigning value to it 给它赋值
<?php
session_start();
if(isset($_REQUEST['variable']) && isset($_REQUEST['value']))
{
$variable = $_REQUEST['variable'];
$value = $_REQUEST['value'];
$_SESSION[$variable] = $value;
}
?>
You can do something like this 你可以做这样的事情
<script src='javascript.php'></script>
Then in the script you can write this for accessing session variable 然后,您可以在脚本中编写此代码以访问会话变量
<?php header("Content-type: application/javascript"); ?>
$(function() {
$( "#progressbar" ).progressbar({
value: <?php echo $_SESSION['value'] ?>
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.