[英]check php session by JavaScript ,if it does not exist should not cause any error
var secret_photo
var secret_name
if (!<?php echo isset($_SESSION['fb_item'])?'true':'false'; ?>) {
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
} else {
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
}
I need to set php
session to html
using JavaScript
.There is 2 logins , one is the facebook
and the other is my own login system, facebook
uses session and my own login sets cookie. 我需要使用
JavaScript
将php
session设置为html
。有2个登录名,一个是facebook
,另一个是我自己的登录系统, facebook
使用session和我自己的登录集cookie。
When I login by facebook
, things are good , but if I login using my own login system , that echo check is going to cause problem , so how am I going to fix it ? 当我通过
facebook
登录时,一切都很好,但是如果我使用自己的登录系统登录,该回显检查将引起问题,那么我该如何解决呢? Check if the session is available or not even if it doesn't exist it wont cause any problem. 检查会话是否可用,即使它不存在也不会引起任何问题。
You are probably encounter an error at the bottom of your code. 您可能在代码底部遇到错误。 You can't access
$_SESSION['fb_item']
if it doesn't exist, it either shows a Notice
or generate wrong js code secret_photo = ;
如果
$_SESSION['fb_item']
不存在,则无法访问它,它会显示一个Notice
或生成错误的js代码secret_photo = ;
. 。 Just move condition to
php
side: 只需将条件移到
php
端:
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])):?>
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php else:?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php endif;?>
<script type="text/javascript">
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])){?>
if($.cookie('user')){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php }else{?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php }?>
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.