简体   繁体   English

单击按钮时,检查用户是否已登录

[英]On click of button, check if user is logged in or not

I have a button that is responsible for performing multiple task, and has lot of scripting for it. 我有一个按钮,它负责执行多个任务,并为此编写了很多脚本。 When this button 当这个按钮

<button type="submit" value="submit" name="submit" class="second_button">Negotiate</button>

the script applied on this button is 应用于此按钮的脚本是

<script>

var startClock;
var submitamt;
var walkaway;
var digits;

$(function() {
  startClock = $('#startClock').on('click', onStart);
  submitamt = $('#submitamt').on('click', onSubmit);
  walkaway = $('#walkaway').on('click', onWalkAway);
  digits = $('#count span');
  beforeStart();
});

var onStart = function(e) {
  startClock.fadeOut(function() {
    startTimer();
    submitamt.fadeIn(function() {
      submitamt.trigger('click'); // fire click event on submit
    });
    walkaway.fadeIn();
  });
};

var onSubmit = function(e) {
  var txtbox = $('#txt').val();
  var hiddenTxt = $('#hidden').val();
  $.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    success: function(returndata) {

         console.log(returndata[3]);
    // $('#proddisplay').html(returndata);
    },
    error: function() { 
      console.error('Failed to process ajax !');
    }
  });
};

var onWalkAway = function(e) {
  console.log('onWalkAway ...');
};

var counter;
var timer;
var startTimer = function() {
  counter = 120;
  timer = null;
  timer = setInterval(ticker, 1000);
};

var beforeStart = function() {
  digits.eq(0).text('2');
  digits.eq(2).text('0');
  digits.eq(3).text('0');
};

var ticker = function() {
  counter--;
  var t = (counter / 60) | 0; // it is round off
  digits.eq(0).text(t);
  t = ((counter % 60) / 10) | 0;
  digits.eq(2).text(t);
  t = (counter % 60) % 10;
  digits.eq(3).text(t);
  if (!counter) {
    clearInterval(timer);
    alert('Time out !');
    resetView();
  }
};

var resetView = function() {
  walkaway.fadeOut();
  submitamt.fadeOut(function() {
    beforeStart();
    startClock.fadeIn();
  });
};
</script>

before this script executes i wish to check if the user is logged in or not, In normal php i use this code 在执行此脚本之前,我希望检查用户是否已登录,在正常的php中,我使用此代码

<?
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true || isset($_SESSION['fb_logged_in']) && $_SESSION['fb_logged_in'] == true )
    {?>
        <!--display message-->
    <?}?>   

but i am not able to understand how to embed it with the script i wish to display a popup message if the user is not logged in and ask user to login first 但我无法理解如何将其嵌入脚本中,如果用户未登录,并希望用户先登录,我希望显示弹出消息

it is not recommended to check it with client side scripting. 不建议使用客户端脚本进行检查。 try it with hidden variable.. 尝试使用隐藏变量。

<input type="hidden" name="userLog" id="userLog" value="<?=$_SESSION['fb_logged_in'];?>">

and in script check 并在脚本中检查

if($('#userLog')!=''){  
  // do something..
}

Embed the Session check in the http post. 将会话检查嵌入到http帖子中。 Return an error code (for example 400) when not logged in and process popup handling in the error function. 未登录时返回错误代码(例如400),并在错误功能中处理弹出窗口。

<?
  if (isset($_SESSION['loggedin']) 
  && $_SESSION['loggedin'] == true || isset($_SESSION['fb_logged_in']) 
  && $_SESSION['fb_logged_in'] == true ) {?> 
       <!-- display message -->
  <?}else{
       http_response_code(400);
    }?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM