Php Sessions and Ajax


Php Sessions and Ajax

在我的index.php文件中,我有一个AJAX函数,它将调用另一个php文件中的一个函数,该函数应该递增一个数字,并在我调用AJAX函数时返回它。

问题是这个数字永远不会改变。我尝试了很多不同的东西。不幸的是,太多了,无法全部列出。

我的index.php文件。

<?php
     session_start();
     $_SESSION['views'] = 0;
 ?>
 <?php include 'blogFunction.php';?>
 <script type="text/javascript">
 function doSomething()
 {
    $.ajax({ url: '/blogFunction.php',
     data: {action: 'test'},
     type: 'post',
     success: function(output) {
     document.getElementById("blog").innerHTML = '';
              document.getElementById("blog").innerHTML = output;
                 }
   });
  }
 </script>
 <div class ="blog" id = "blog"></div>

我的blogFunction.php

<?php 
      if(isset($_POST['action']) && !empty($_POST['action'])) {
         $action = $_POST['action'];
         switch($action) {
            case 'test' :  blogreturn();break;
         }
      }
function blogreturn(){
      $_SESSION['views'] = $_SESSION['views']+ 1;
      echo "THIS number is:" .$_SESSION['views'];
}
 ?>

现在,每当我点击调用AJAX函数的按钮时,输出总是"1"。

感谢您的帮助。

实时代码:此处

感谢大家到目前为止的帮助。一个问题出现,一个新问题出现。

扩展功能:

session_start();
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
    case 'test' :  blogreturn();break;
}

}

function blogreturn(){  
    $request_url = "http://retrovate.tumblr.com/api/read?type=posts";
    $xml = simplexml_load_file($request_url);
    
    $a = $_SESSION['views'];
    $b = $_SESSION['views'] +4;
    echo "A = ".$a;
    echo "B = ".$b;
    $_SESSION['views'] = $_SESSION['views']+ 1;
    for ($i = $a; $i <= $b; $i=$i+1) {
            echo '<h2>'.$xml->posts->post[$i]->{'regular-title'}.'</h2>';
            echo '<br>';
            echo $xml->posts->post[$i]->{'regular-body'};
            echo '<br>';
            echo '<br>';
    }    
}

这里的问题是,我在我的网站上点击了一次按钮

并且它递增并显示新内容。我再次单击,它将恢复为0。如果我快速点击按钮无数次,它似乎会起作用。chrome似乎有这个问题,而Firefox则没有。

session_start();添加到blogFunction.php

以下是正常工作的代码。。。index.php

<?php 
 session_start();
 $_SESSION['views'] = 0;
 ?>
<!doctype html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js" />
    <body>
        <div class ="blog" id = "blog"></div>
        <input type="button" value="Add to the count!" id="call_ajax"/>
        <script type="text/javascript">
            $('#call_ajax').click(function () {
                $.ajax({ url: '/blogFunction.php',
                    data: {action: 'test'},
                    type: 'post',
                    success: function(output) {
                        document.getElementById("blog").innerHTML = '';
                        document.getElementById("blog").innerHTML = output;
                    }
                });
            });
        </script>
</body>

blogFunction.php

<?php
session_start();
  if(isset($_POST['action']) && !empty($_POST['action'])) {
     $action = $_POST['action'];
     switch($action) {
        case 'test' :  blogreturn();break;
     }
  }
function blogreturn(){
  $_SESSION['views'] = $_SESSION['views']+ 1;
  echo "THIS number is:" .$_SESSION['views'];
}
?>

注意,我没有在index.php文件中包含blogFunction.php!这很重要。

另一种方法是,每次加载页面时都将变量设置为0,这就是函数的调用方式(如果使用控制台进行调用)。

我添加了一个按钮,供您单击以通过Ajax调用该函数(根据您在原始问题中的条件)。

希望能有所帮助!

您需要在blogFunction.php文件中调用session_start()。在向浏览器输出任何内容之前,必须调用它。最好的情况可能是在脚本中首先调用它。

我认为您应该先取消设置$_SESSION['views'],然后再写。

function blogreturn(){
  $temp = $_SESSION['views'];
  unset($_SESSION['views']);
  $_SESSION['views'] = $temp + 1;
  echo "THIS number is:" .$_SESSION['views'];
}