在我原来的php5代码中,一旦用户打开索引页面,就会有一个ajax调用来检查是否存储了<?php
include_once('../includes/connect_database.php');
include_once('../includes/variables.php');
if(isset($_GET['accesskey']) && isset($_GET['category_id'])) {
$access_key_received = $_GET['accesskey'];
$category_ID = $_GET['category_id'];
if(isset($_GET['keyword'])){
$keyword = $_GET['keyword'];
}else{
$keyword = "";
}
if($access_key_received == $access_key){
if($keyword == ""){
// find menu by category id in menu table
$sql_query = "SELECT Menu_ID, Menu_name, Price, Menu_image
FROM tbl_menu
WHERE Category_ID = ".$category_ID."
ORDER BY Menu_ID DESC";
}else{
// find menu by category id and keyword in menu table
$sql_query = "SELECT Menu_ID, Menu_name, Price, Menu_image
FROM tbl_menu
WHERE Menu_name LIKE '%".$keyword."%' AND Category_ID = ".$category_ID."
ORDER BY Menu_ID DESC";
}
$result = $connect->query($sql_query) or die("Error : ".mysql_error());
$menus = array();
while($menu = $result->fetch_assoc()) {
$menus[] = array('Menu'=>$menu);
}
// create json output
$output = json_encode(array('data' => $menus));
}else{
die('accesskey is incorrect.');
}
} else {
die('accesskey and category id are required.');
}
//Output the output.
echo $output;
include_once('../includes/close_database.php');
?>
。如果有会话,将显示用户。否则,页面将重定向到登录页面。一旦我升级到php 7,它就会停止工作。
$_SESSION['user']
$.ajax({
type: 'POST',
url: "php/checksession.php",
})
.done(function(response) {
console.log(response);
var code = response[0].code;
///// if no loging session is stored, redirect back to loging page
if (code == 0) {
window.location.href = "login";
///// Session is found, update balance
} else {
$('body').show();
///////////////////// Do other stuff
}
}
这是php7代码。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Title Goes Here</title>
</head>
<body style="display:none">
<p>PageContent</p>
</body>
</html>
<?php
header('Content-type: application/json');
require 'connection.php';
// Get Access to DB
// if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST))
{
ob_start();
session_start();
if (isset($_SESSION['user']))
{
$responseArray = array(
'code' => '1',
'type' => 'good',
'message' => 'found'
);
}
else
{
$responseArray = array(
'code' => '0',
'type' => 'bad',
'message' => 'no session'
);
}
}
else
{
$responseArray = array(
'code' => '0',
'type' => 'bad',
'message' => 'no POST'
);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$Arrays = [];
array_push($Arrays, $responseArray);
$encoded = json_encode($Arrays);
header('Content-Type: application/json');
echo $encoded;
}
else
{
echo $responseArray['message'];
} // else just display the message
?>
为空,并且由于不满足以下条件,因此Json数据不会发送回浏览器:
$_POST
答案 0 :(得分:0)
尝试使用另一种方法来获取POST数据。 我不知道为什么,但是当我使用AJAX时,POST数据有时会以另一种方式...
<?php
if(!isset($_POST)) {
$_POST = json_decode(file_get_contents("php://input"));
}