allex01 Posted February 6, 2009 Share Posted February 6, 2009 I have a page that looks like test.php?rand=1. If a user refreshed the page by pressing f5 i want rand=1 to change value to a random number. Is there a way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/ Share on other sites More sharing options...
gizmola Posted February 6, 2009 Share Posted February 6, 2009 yes, did you look at the php rand() function? Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-756231 Share on other sites More sharing options...
pickled Posted February 6, 2009 Share Posted February 6, 2009 I'm confused. There's a way to get the url of the page to change on refresh? I thought a refresh doesn't touch the url. Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-756247 Share on other sites More sharing options...
allex01 Posted February 6, 2009 Author Share Posted February 6, 2009 I don't think you understood the question. pickled is correct, i don't think we could change that in URL, but i was asking if there was a way to do so. I have this code <?php $w=rand(); ?> <a href="test.php?ran=<?php echo $w; ?>">test</a> When you click on the test link, a page will be generated like test.php?ran=321234 Now if a user refreshes this page by pressing f5, i want the ran= to change automatically to another random number in the url without having to click on the test link again. I was asking if this can be done somehow. Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-756249 Share on other sites More sharing options...
aebstract Posted February 6, 2009 Share Posted February 6, 2009 Why would you need to do that? Why don't you just do a $ran = rand(); in your page? I don't see a reason to use it from the url, if it's gonna be generated randomly all the time anyway? Then every time you refresh the page, $ran will be different.. edit: If you need to set ran in the url to determine if something has been done, you can do something like this: test.php?ran if (isset($_GET['ran'])) { $ran = rand(); } Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-756254 Share on other sites More sharing options...
gizmola Posted April 21, 2009 Share Posted April 21, 2009 It's still possible. What would be required is the use of a header command. You'd probably have to set a cookie so that the system knew that this was not the first time the page was visited. Don't know what the application of this idea is good for, since you haven't provided any detail. Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-815733 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 You will also want to round the number: echo '<a href="http://somesite.com/page.php?rand='.round(rand(1, 1000)).'">Link</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-815752 Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 Not needed, rand produces an int: http://uk2.php.net/rand Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-815755 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 your right... I once was getting decimals using rand hmm btw... you can place [ m]rand[/m] to make a link to the manual Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-815764 Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 That rings a bell with me too, not sure why. I only know because I used it the other day and specifically checked. Ta for the manual advice, didn't know that, will save me some time Quote Link to comment https://forums.phpfreaks.com/topic/144124-random-url-id-upon-refresh/#findComment-815776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.