如果未设置$ _SESSION ['SESS_USER_ID'],则触发或阻止JavaScript函数 [英] Triggering or preventing a javascript function if $_SESSION['SESS_USER_ID'] isn't set

查看:128
本文介绍了如果未设置$ _SESSION ['SESS_USER_ID'],则触发或阻止JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个php函数没有登录,那么它现在将用户重定向到另一个页面。

  if(!isset($ _SESSION ['SESS_USER_ID'])||(trim($ _ SESSION ['SESS_USER_ID'])=='')){
header(location:login_failed.html);
exit();

$ / code>

如何更改函数以便重定向用户而不是重定向以下内容:


  • 触发javascript函数: loginFailed();

  • 防止预加载的特定iframe启动(这些需要用户登录,并且如果它们未登录,则当前打开为空iframe)。我已经在下面包含了这个脚本。或者,防止启动具有特定类或数据fancybox类型的iframe,或者需要!isset($ _ SESSION ['SESS_USER_ID'])
  • >


我想触发/阻止的函数

  function loginFailed(){
$()。toastmessage('showToast',{
text:'登录失败< br />< br />请检查您的用户名和密码',
sticky:false,
位置:'top-center',
类型:'message',
closeText:'',
close:function() {console.log(toast is closed ...)}
});
}
$(function(){
$(。fancybox)。fancybox({
preload:true,
});
$ (.fancybox)。eq(0).trigger('click');
});

我试过了:

  echo< script> loginFailed();< / script>; 
echo< script type =text / javascript> loginFailed();< / script>;
echo< script type =text / javascript> loginFailed;< / script>;


解决方案

我将@ hellohellosharp的建议与给出的解决方案相结合给我的@willoller另一个问题,并计算出来: - )



添加到php:

  $ logged_in =(isset($ _ SESSION ['SESS_USER_ID'])); 

将其添加到javascript:

 <?php if($ logged_in):?> 
< a class =fancyboxdata-fancybox-type =iframehref =iframe1.html>< / a>
<?php else:?>
loginFailed();
<?php endif; ?>

我也使用它来根据用户是否登录来更改菜单选项。

提醒一下 - 它使用ISSET来确定JavaScript应显示的内容,但它只会改变用户的体验,并不会提供任何安全性。

This php function currently redirects the user to another page if they aren't logged in

if(!isset($_SESSION['SESS_USER_ID']) || (trim($_SESSION['SESS_USER_ID']) == '')){
header("location: login_failed.html");
exit();
} 

How can I change the function so that instead of redirecting the user it does the following:

  • Triggers the javascript function: loginFailed();
  • Prevents specific iframes which are preloaded from launching (these require a user to be logged in, and are currently opening as empty iframes if they're not logged in). I've included the script for this below. Alternatively, preventing iframes from launching which have a specific class or data-fancybox-type, or those which require !isset($_SESSION['SESS_USER_ID']).

The functions which I want to trigger/prevent

function loginFailed() {
    $().toastmessage('showToast', {
        text: 'Log In Failed<br/><br/>Please check your username and password',
        sticky: false,
        position: 'top-center',
        type: 'message',
        closeText: '',
        close: function () { console.log("toast is closed ...") }
    });
} 
$(function () {
    $(".fancybox").fancybox({
      preload: true,
    });
    $(".fancybox").eq(0).trigger('click');
});   

I've tried:

echo "<script>loginFailed();</script>";
echo "<script type="text/javascript">loginFailed();</script>";
echo "<script type="text/javascript">loginFailed;</script>";

解决方案

I combined @hellohellosharp's suggestion with a solution which was given to me by @willoller for another problem and figured it out :-)

Add this to the php:

$logged_in = (isset($_SESSION['SESS_USER_ID']));

Add this to the javascript:

<?php if ($logged_in) : ?>
    <a class="fancybox" data-fancybox-type="iframe" href="iframe1.html"></a>
<?php else : ?>  
     loginFailed();
<?php endif; ?>

I'm also using this to change the menu options based on whether or not a user's logged in.

Just a reminder - this uses ISSET to determine what javascript should show, but it only changes the user's experience and doesn't provide any security.

这篇关于如果未设置$ _SESSION ['SESS_USER_ID'],则触发或阻止JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆