Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking a cookie in a PHP script
    primarykey
    data
    text
    <p>Hey i still need a little bit of help with the rating system but i have a problem checking if the cookie exist this is </p> <pre><code>&lt;?php $rating = new ratings($_POST['widget_id']); isset($_POST['fetch']) ? $rating-&gt;get_ratings() : $rating-&gt;vote(); class ratings { var $data_file = './ratings.data.txt'; private $widget_id; private $data = array(); function __construct($wid) { $this-&gt;widget_id = $wid; $all = file_get_contents($this-&gt;data_file); if($all) { $this-&gt;data = unserialize($all); } } public function get_ratings() { if($this-&gt;data[$this-&gt;widget_id]) { echo json_encode($this-&gt;data[$this-&gt;widget_id]); } else { $data['widget_id'] = $this-&gt;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-&gt;widget_id; # Update the record if it exists # doesn't update if it already exists if (!isset($_COOKIE[$id])) { if($this-&gt;data[$ID]) { $this-&gt;data[$ID]['number_votes'] += 1; $this-&gt;data[$ID]['total_points'] += $vote; } # Create a new one if it doesn't else { $this-&gt;data[$ID]['number_votes'] = 1; $this-&gt;data[$ID]['total_points'] = $vote; } $this-&gt;data[$ID]['dec_avg'] = round( $this-&gt;data[$ID]['total_points'] / $this-&gt;data[$ID]['number_votes'], 1 ); $this-&gt;data[$ID]['whole_avg'] = round( $this-&gt;data[$ID]['dec_avg'] ); file_put_contents($this-&gt;data_file, serialize($this-&gt;data)); $this-&gt;get_ratings(); } } //ends if isset # --- # end class } ?&gt; </code></pre> <p>The part that should check if the cookie exist is this:</p> <pre><code>if (!isset($_COOKIE[$id])) { </code></pre> <p>but for some reason nothing in the script works when i add that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload