Search code examples
phpsessiongetvoting

How can I set a session for multiple links that I want to disable?


piece of file1.php:

<a href="file2.php?addid=1">Add this to DB</a>

This takes the user to a page where the data gets inserted into the database.

file2.php :

 $clicked = $_GET['addid'];
 $_SESSION['clicked'] = $clicked;
 // data gets inserted
 header("Location: file1.php?id=$clicked");

But I have got multiple pages (like: file1.php?id=1 | file1.php?id=2 | file1.php?id=3 etc.). Can the session variable handle multiple numbers? Is there any way to do this?

Any help appreciated.

(P.S.: Currently I am using the GET method to disable the links, but I think SESSION is more reliable.) (P.P.S.: I need this for a voting script.)


Solution

  • To hold more data in one session variable, you need to create a multi-dimensional array which will hold multiple against $_SESSION['checked']. You can do this like:

    $clicked = (int)$_GET['addid'];
    $_SESSION['clicked'][$clicked] = true;
    // data gets inserted
    header("Location: file1.php?id=$clicked");
    

    (also, you should be sanitizing $_GET['addid'].

    Then to check if it is set, you can use array_key_exists:

    if(array_key_exists($clicked,$_SESSION['clicked'])){
      echo "this button should be disabled!";
    }