2

I am working on pages which are secured so no-one can link to that page using this:

Code below is called inside a loop.

$gentok = uniqid();

if(isset($_GET["action"]) && $_GET["action"] == "clean_$gentok") {
    // stuff
}

Then, I have this to call the URL:

<a href="<?php echo admin_url("themes.php?page=cleaner&action=clean_$gentok"); ?>">Clean this and that</a>

But when clicking the link, the page refreshes and the uniqid() has already changed.

How can I make it so the uniqid() is still the same after the page refresh? I'm open for any changes or better ideas you may have.

Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
J. Doe
  • 503
  • 1
  • 6
  • 19

3 Answers3

1

Posting this as a community wiki since I've nothing to gain from this.

My suggestion in comments about using a nonce brought the OP to use the WordPress version of a nonce as their solution.

Reference:

Sidenote: To be honest, I was not aware that WordPress had one and found that reference link on the Internet.

My original reference:

Additional reference:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Use session for this. Put your unique ID in session array

session_start();

$_SESSION['gentok'] = uniqid();

if (isset($_GET["action"]) && $_GET["action"] == "clean_" . $_SESSION['gentok']) {
    // stuff
}

In your display

session_start();

<a href="<?= admin_url('themes.php?page=cleaner&action=clean_' . $_SESSION['gentok']) ?>">Clean this and that</a>
Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
  • Okay, thank you for the answer. Will this also work in `foreach`? – J. Doe Aug 05 '16 at 22:18
  • Not too crazy, just `foreach ($theme_names as $theme_name) {` and inside that the code in the question – J. Doe Aug 05 '16 at 22:22
  • I have found that a nonce system is kind of better for this, I might have not explained it that well in my question. Thank you anyway for your help and effort. :) – J. Doe Aug 05 '16 at 22:42
0

When you creating a session set a value so every time that page loads it will check is your session for the value. Else you will redirect......you would put the code on top. If($_SESSION['sesname']!=$value]{header location}

You would pit this at the top of the page so it performs the check

OR If you want a unique name then just put something that people want easily guess and don't link it any where

Reuben Gomes
  • 878
  • 9
  • 16