user3666355
user3666355

Reputation: 83

Insert random number into URL when user clicks on button and hyperlink to go to another page

I have a time sheet project where employees clock in and clock out. If it is after 4pm, they have to submit a duty log before the clock out button appears.

The project is complete and functions like it should, but I found some flaws in which the employee can bypass having to fill-out the duty log by actually completing it one time and saving the URL of the duty log submission page.

Also the user can even save the clock out URL so they won't have to fill out their duty log (they can just enter the clock out URL when asked to fill it out). I want to prevent employees from doing this by having a random number inserted to a url so when the user clicks the URL or hyperlink, the URL contains a random number each time.

I tried both of these but it didn't work. Am I on the right track? Ideas and suggestions appreciated. If you can, can you please post an example? Thanks in advance.

Hyperlink:

var url = "http://www.mypage.com/index.php?"+Math.random()

<a href="http://www.mypage.com/index.php?1" onClick="this.href=this.href.split('?')[0]+'?'+new Date().getTime()">Mostly random</a>

Button:

echo "<form action='ClockOut.php'+Math.random() method='post'>
            <span style=\"background-color: #FFFF00\">**Please Clock Out when your shift ends**</span>
            <input value='Clock Out' id='Submit2' type='submit' /></td>               
            </form>";

Upvotes: 1

Views: 1005

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

There are many ways of doing this using a server-side PHP method, yet here is one example:

<a href="timekeeper.php?var<?php echo uniqid() . date('_m_d_Y_h_i_s_a', time());?>">Log time</a>

Which will produce something like this when clicking on the link:

http://www.example.com/timekeeper.php?var53e4137591352_08_07_2014_08_01_57_pm

The breakdown:

?var being taken from an isset($_GET)

if(isset($_GET["var"])) {

    echo "SUCCESS";

}

then,

  • 53e4137591352 generated by uniqid() which will change each time and is unique.

  • _08_07_2014_08_01_57_pm generated by time() changes also.

    • You can use UNIX TIMESTAMP also.

Functions used:

So, you could replace uniqid() with the employee's ID number if needed (for example), as well as tack on any other additional random-based or time functions, just as long as they are properly formatted and concatenated.

I hope this helps.

Upvotes: 2

user3446786
user3446786

Reputation: 98

Fred -ii- is right. It is better to use Server-side solution(PHP in this case) as JS can be disabled. Within a few minutes I was able to find solutions for both cases. Wonder if you have made any research.

SOLUTION 1(PHP):

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}

SOLUTION 2(JS):

  function makeid()
 {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for( var i=0; i < 5; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

Upvotes: 1

Related Questions