Random URL Redirect On click
I want to redirect my links random every time they click on Hyper-link or button or Image on my website.
Example :
I have 3 URL : Google.com, Facebook.com, yahoo.com.
<a href="<?php $sites[array_rand($sites)] ?>">Visit here</a>
So whenever user click on visit here they will taking to random from anyone of the 3 URL. i have tried this code but not working as i want, it simply making a redirect.
$sites = array(
'http://www.google.com/',
'http://www.facebook.com/',
'http://www.yahoo.com/'
);
die();
Please help me in this. Provide me working JavaScript or PHP code.
javascript php jquery html ajax
add a comment |
I want to redirect my links random every time they click on Hyper-link or button or Image on my website.
Example :
I have 3 URL : Google.com, Facebook.com, yahoo.com.
<a href="<?php $sites[array_rand($sites)] ?>">Visit here</a>
So whenever user click on visit here they will taking to random from anyone of the 3 URL. i have tried this code but not working as i want, it simply making a redirect.
$sites = array(
'http://www.google.com/',
'http://www.facebook.com/',
'http://www.yahoo.com/'
);
die();
Please help me in this. Provide me working JavaScript or PHP code.
javascript php jquery html ajax
2
you're missing a semi-colon after$sites = array(...);
– Emissary
Nov 11 at 15:29
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38
add a comment |
I want to redirect my links random every time they click on Hyper-link or button or Image on my website.
Example :
I have 3 URL : Google.com, Facebook.com, yahoo.com.
<a href="<?php $sites[array_rand($sites)] ?>">Visit here</a>
So whenever user click on visit here they will taking to random from anyone of the 3 URL. i have tried this code but not working as i want, it simply making a redirect.
$sites = array(
'http://www.google.com/',
'http://www.facebook.com/',
'http://www.yahoo.com/'
);
die();
Please help me in this. Provide me working JavaScript or PHP code.
javascript php jquery html ajax
I want to redirect my links random every time they click on Hyper-link or button or Image on my website.
Example :
I have 3 URL : Google.com, Facebook.com, yahoo.com.
<a href="<?php $sites[array_rand($sites)] ?>">Visit here</a>
So whenever user click on visit here they will taking to random from anyone of the 3 URL. i have tried this code but not working as i want, it simply making a redirect.
$sites = array(
'http://www.google.com/',
'http://www.facebook.com/',
'http://www.yahoo.com/'
);
die();
Please help me in this. Provide me working JavaScript or PHP code.
javascript php jquery html ajax
javascript php jquery html ajax
edited Nov 12 at 6:56
asked Nov 11 at 15:21
Rajeev Ranjan Sharma
248
248
2
you're missing a semi-colon after$sites = array(...);
– Emissary
Nov 11 at 15:29
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38
add a comment |
2
you're missing a semi-colon after$sites = array(...);
– Emissary
Nov 11 at 15:29
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38
2
2
you're missing a semi-colon after
$sites = array(...);
– Emissary
Nov 11 at 15:29
you're missing a semi-colon after
$sites = array(...);
– Emissary
Nov 11 at 15:29
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38
add a comment |
2 Answers
2
active
oldest
votes
The same functionality using javascript:
<a href='javascript:openUrl()'>Visit here</a>
<script>
var sites=['http://www.google.com/',
'http://www.msn.com/',
'http://www.yahoo.com/'
];
function openUrl(){
var i = Math.round(Math.random()*(sites.length-1));
window.location.href=sites[i];
return false;
}
</script>
add a comment |
I got my code working.
<?php
$addresses = [
'http://www.google.com',
'http://www.facebook.com',
'http://www.youtube.com'
];
$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];
?>
<a href="<?php echo $randomUrl; ?>">random url</a>
If you have a better code, Please make a suggestion.
Thank you
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250161%2frandom-url-redirect-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The same functionality using javascript:
<a href='javascript:openUrl()'>Visit here</a>
<script>
var sites=['http://www.google.com/',
'http://www.msn.com/',
'http://www.yahoo.com/'
];
function openUrl(){
var i = Math.round(Math.random()*(sites.length-1));
window.location.href=sites[i];
return false;
}
</script>
add a comment |
The same functionality using javascript:
<a href='javascript:openUrl()'>Visit here</a>
<script>
var sites=['http://www.google.com/',
'http://www.msn.com/',
'http://www.yahoo.com/'
];
function openUrl(){
var i = Math.round(Math.random()*(sites.length-1));
window.location.href=sites[i];
return false;
}
</script>
add a comment |
The same functionality using javascript:
<a href='javascript:openUrl()'>Visit here</a>
<script>
var sites=['http://www.google.com/',
'http://www.msn.com/',
'http://www.yahoo.com/'
];
function openUrl(){
var i = Math.round(Math.random()*(sites.length-1));
window.location.href=sites[i];
return false;
}
</script>
The same functionality using javascript:
<a href='javascript:openUrl()'>Visit here</a>
<script>
var sites=['http://www.google.com/',
'http://www.msn.com/',
'http://www.yahoo.com/'
];
function openUrl(){
var i = Math.round(Math.random()*(sites.length-1));
window.location.href=sites[i];
return false;
}
</script>
answered Nov 11 at 15:47
Pati
43748
43748
add a comment |
add a comment |
I got my code working.
<?php
$addresses = [
'http://www.google.com',
'http://www.facebook.com',
'http://www.youtube.com'
];
$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];
?>
<a href="<?php echo $randomUrl; ?>">random url</a>
If you have a better code, Please make a suggestion.
Thank you
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
add a comment |
I got my code working.
<?php
$addresses = [
'http://www.google.com',
'http://www.facebook.com',
'http://www.youtube.com'
];
$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];
?>
<a href="<?php echo $randomUrl; ?>">random url</a>
If you have a better code, Please make a suggestion.
Thank you
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
add a comment |
I got my code working.
<?php
$addresses = [
'http://www.google.com',
'http://www.facebook.com',
'http://www.youtube.com'
];
$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];
?>
<a href="<?php echo $randomUrl; ?>">random url</a>
If you have a better code, Please make a suggestion.
Thank you
I got my code working.
<?php
$addresses = [
'http://www.google.com',
'http://www.facebook.com',
'http://www.youtube.com'
];
$size = count($addresses);
$randomIndex = rand(0, $size - 1);
$randomUrl = $addresses[$randomIndex];
?>
<a href="<?php echo $randomUrl; ?>">random url</a>
If you have a better code, Please make a suggestion.
Thank you
answered Nov 11 at 15:39
Rajeev Ranjan Sharma
248
248
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
add a comment |
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
2
2
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
I would recommend Pati's answer as it doesn't require refreshing the page for the URL to be different with each click. Since PHP is processed server-side, the URL isn't dynamic one it's sent to the client. Pati's JS answer dynamically changes the URL on the client.
– Snake14
Nov 11 at 15:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250161%2frandom-url-redirect-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
you're missing a semi-colon after
$sites = array(...);
– Emissary
Nov 11 at 15:29
Also not clear if that href is legitimate or not. You haven't identified where the current redirect takes you
– charlietfl
Nov 11 at 15:38