如何查看是否使用jquery设置了php session [英] how to see if php session has been set by using jquery
问题描述
我是否必须做类似var session = "<?php = $_SESSION['sessionname'] ?>";
的操作
还是有一个jquery语句?
我希望能够使用jquery检查会话是否存在
我需要这样做,因为当用户输入用户名时,ajax会加载一个php文件来对其进行验证.如果电子邮件不匹配,则此php启动会话.我尝试了JSON,但它似乎对我不起作用.
我也很高兴听到其他解决方法:)
Do i have to do something like var session = "<?php = $_SESSION['sessionname'] ?>";
Or is there a jquery statement for it?
I want to be able to check if the session exists using jquery
I need to do this because, when the user inputs his username, an ajax loads a php file to validate it. This php starts a session if the email does not match. I tried JSON but it did not seem to work for me.
I will be happy to hear any other way to solve this as well :)
推荐答案
data = {
action: "session"
}
$.ajax({
type: "POST",
dataType: "json",
url: ajaxurl,
data: data,
success: function (json) {
console.log(json.sessionvalue);
if(json.session == "true"){
// code here
}
}
});
在php端
if(isset($_POST['action']) && $_POST['action'] == 'session'){
session_start();
return json_encode(array("sessionvalue" => $_SESSION['mysessionvar']));
}
您还可以分配多维数组
$array = array("sessions"=>array('value1', 'value2', 'value3'), 'numbers'=>array(0,5,6,8));
return json_encode($array);
可以使用json.sessions [0]等来检索值.
Values can be retrieved by using json.sessions[0] etc ..
提交ajax调用时,应始终使用控制台
When you commit ajax calls, you should always work with the console
这篇关于如何查看是否使用jquery设置了php session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!