如果执行手动刷新,如何执行功能

时间:2014-01-16 15:21:19

标签: php jquery

索引页面设计得如此美妙。它通过jquery请求登录后检索数据......我的疑问是如果在执行手动刷新时如何维护页面?如果执行手动刷新,我可以执行任何功能吗?这是我的一些代码..

Checklogin.php

    <?php
           session_start();
           include('dbconnect.php');
           $email=mysql_real_escape_string($_REQUEST['email']);
           $pass=mysql_real_escape_string($_REQUEST['pass']);
           $pass=filter_var($pass, FILTER_SANITIZE_STRING);
           $email=filter_var($email, FILTER_SANITIZE_EMAIL);
           $query="SELECT * from `users` WHERE `email`='$email' and `pass`='$pass'";
           $res=mysql_query($query) or die("Error in query");
           $res1=mysql_num_rows($res);
           $name="guest";
           if($res1 == 0)
           {    
          $query="SELECT * from `users` WHERE `email`='$email'";
          $res=mysql_query($query) or die("Error in query");
          $res2=mysql_num_rows($res);
          if($res2>0){
        $code=3;
        echo json_encode(array('name'=>$name,'code'=>$code));
          }
          else{
        $code=2;
        echo json_encode(array('name'=>$name,'code'=>$code));
          }
        }
        else
        {
    $code=1;
    $row = mysql_fetch_assoc($res);
    $name=$row['name'];
    $_SESSION['email']=$email;
    $_SESSION['name']=$name;
    $query="select * from crown1 where email='".$_SESSION['email']."';";
                    $res=mysqli_query($con,$query);
                    $row=mysqli_fetch_array($res);
                    $level=$row['level'];
                    $points=$row['points'];
                    $rank=$row['rank'];
                    $times="time".$level;
                    $timest=$row[$times];
                    $query="select * from qna where level=".$level.";";
                    $res9=mysqli_query($con,$query);
                    $row9=mysqli_fetch_array($res9);
                    $title=$row9['title'];
                    $qn=$row9['qnlink'];
                    $_SESSION['title']=$title;
                    $_SESSION['qnlink']=$qn;
                    $_SESSION['level']=$level;
                    $_SESSION['points']=$points;
                    $_SESSION['timest']=$timest;
                    $_SESSION['rank']=$rank;

    //echo $_SESSION['email'];
    echo json_encode(array('name'=>$_SESSION['name'],'code'=>$code));

    }
?>

leader.js

    var loadData = function(){
        var url = 'Leaders.php';
        $.getJSON(url, function(data) {
        $('#Leaders').empty().append('<ul style="position: absolute; margin: 0px; padding: 0px; top: 0px; list-style-type:none; width:100%">');
        $.each(data, function(index, data) {
           $('#Leaders>ul').append("<li style='margin: 0px; padding: 0px; height: 30px; width:100%'>&nbsp; #"+data.rank+" &nbsp;&nbsp;"+data.name+"<span class='myNumber' name='n1' style='float:right;'> "+data.points+" &nbsp;</span></li>");
        });
          $('#Leaders').append('</ul>');
   });
      var url = 'Game.php';
      $.getJSON(url, function(data) {
        $('#points').empty().append(data.points);
        $('#level').empty().append(data.level);
        $('#ranking').empty().append(data.rank);
        $('#levelUsers').empty().append(data.users);
        $('#totalPlayers').empty().append(data.nousers);
        $('#titlediv>div>h3>center').empty().append(data.title);
        $('#game3').empty().append("<img src='"+data.qnlink+"' style='width:700px;height:250px;'/>");
        //setIntervalOnce(loadData, 5000);
      });
}

我会在调用Checklogin后调用LoadData()...

1 个答案:

答案 0 :(得分:0)

没有办法100%检测页面是否手动加载,因为用于确定此信息的信息总是会被欺骗,如果有人真的想要的话。但你可以这样做:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    /* Page was loaded via AJAX */
} else {
    /* Page wasn't loaded via AJAX, e.g. direct url entered, page refreshed, referred from another page. */
}