在PHP脚本中检查cookie
嘿,我仍然需要一点关于评级系统的帮助,但是我在检查cookie是否存在时遇到了一个问题这是在PHP脚本中检查cookie,php,javascript,jquery,ajax,cookies,Php,Javascript,Jquery,Ajax,Cookies,嘿,我仍然需要一点关于评级系统的帮助,但是我在检查cookie是否存在时遇到了一个问题这是 <?php $rating = new ratings($_POST['widget_id']); isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote(); class ratings { var $data_file = './ratings.data.txt';
<?php
$rating = new ratings($_POST['widget_id']);
isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();
class ratings {
var $data_file = './ratings.data.txt';
private $widget_id;
private $data = array();
function __construct($wid) {
$this->widget_id = $wid;
$all = file_get_contents($this->data_file);
if($all) {
$this->data = unserialize($all);
}
}
public function get_ratings() {
if($this->data[$this->widget_id]) {
echo json_encode($this->data[$this->widget_id]);
}
else {
$data['widget_id'] = $this->widget_id;
$data['number_votes'] = 0;
$data['total_points'] = 0;
$data['dec_avg'] = 0;
$data['whole_avg'] = 0;
echo json_encode($data);
}
}
public function vote() {
# Get the value of the vote
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
$ID = $this->widget_id;
# Update the record if it exists
# doesn't update if it already exists
if (!isset($_COOKIE[$id])) {
if($this->data[$ID]) {
$this->data[$ID]['number_votes'] += 1;
$this->data[$ID]['total_points'] += $vote;
}
# Create a new one if it doesn't
else {
$this->data[$ID]['number_votes'] = 1;
$this->data[$ID]['total_points'] = $vote;
}
$this->data[$ID]['dec_avg'] = round( $this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1 );
$this->data[$ID]['whole_avg'] = round( $this->data[$ID]['dec_avg'] );
file_put_contents($this->data_file, serialize($this->data));
$this->get_ratings();
}
} //ends if isset
# ---
# end class
}
?>
但由于某些原因,当我添加这些时,脚本中的任何内容都不起作用
if (!isset($_COOKIE[$id])) {
您将所有内容声明为$ID
,但您使用的是$ID
。试着把它关掉,看看是否有效
if (!isset($_COOKIE[$ID])) {
我想你想做的是设定(如果存在的话)intead!isset(如果不存在)
我没有检查你所有的代码,但是你的$id和$id混合在一起,这可能会导致问题。它应该只在cookie不存在的情况下运行脚本,所以没有任何问题。哎呀,看起来有人在评论中回答了这个问题。他得到了荣誉。但这看起来确实是你的问题,所以我不谈了。
if (!isset($_COOKIE[$ID])) {
if (isset($_COOKIE[$id])) {