Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7163205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:51:47+00:00 2026-05-28T13:51:47+00:00

The following code is within an ajax call. I’m trying to make sure people

  • 0

The following code is within an ajax call. I’m trying to make sure people don’t vote on questions with a certain id too often using sessions.

So they click a button, which executes the following php code:

  $id=$_GET["id"];


if ((isset($_SESSION["$id"]) && ((time() - $_SESSION["$id"]) > 180)) || (!isset($_SESSION["$id"]))) {
    // last vote was more than 3 minutes ago
    $_SESSION["$id"] = time(); // update/create vote time stamp


    //there is code here to add the vote to the database
}
else{
echo "sorry, you've already voted recently";
}

So I’m creating a session variable for each question id which holds the time() of their last vote. I would do this with cookies, but they can be disabled.

Currently, there is a bug somewhere with my logic, because it allows the user to keep clicking the button and adding as many votes as they want.

Can anyone see an error that I have made?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:51:48+00:00Added an answer on May 28, 2026 at 1:51 pm
    1. using sessions to prevent multiple voting makes very little sense.
    2. sessions do use cookies with the same drawbacks
    3. unlike strings, variables in PHP should be addressed without quotes. such a false usage WILL cause an error someday.
    4. I see no point in checking for isset($_SESSION[$id]) twice.
    5. There was a bug in PHP which disallowed numerical indices for the $_SESSION array. Dunno if it was corrected nowadays.
    6. As it was pointed out by Sajid, you have to call session_start() before using $_SESSION array.

    now to the logic.
    to me, it seems the code won’t let anyone to vote at all. as it won’t pass isset($_SESSION[$id]) condition for the first time and won’t let $_SESSION[$id] to be set and so on.

    it seems correct condition would be

    if ( (!isset($_SESSION['vote'][$id]) OR (time() - $_SESSION['vote'][$id]) > 180) ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the following code from within Eclipse: Process process =
How to call webservice through jquery... I tried following code.. $(document).ready(function() { $(#sayHelloButton).click(function(event){ $.ajax({
I have the following code in my main page, trying to pull AJAX JSON.
I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND
I'm using the following code within XCode, building for iOS with ARC enabled. Why
The following code will search for the user within the domain controller, but I
I have the following code. Within the same block of code, I disable a
In the following code, I have verified that connecting to a URL from within
In the following code snippet, the Color enum is declared within the Car class
I have the following code, but i got a problem. Within my While, a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.