<html>
<head>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script>
$('a').click(function(e) {
e.preventDefault();
document.location.href='http://www.redirectlink.com?ref='+this.href;
return false;
});
</script>
</head>
<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://192.168.0.14:8081/home/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);
echo $curl_response;
?>
Should this be working? It curls a page and then when you click on a link on the page it should redirect to http://www.redirectlink.com?ref='+this.href The curling part works but it doesn't redirect. It still takes me to the original url. The jquery-1.6.1.js file is in the same directory as all the other files.
EDIT: Problem fixed. Needed to ready the javascript.
Redirect?
Four issues:
1). You're calling the $('a') selector before the document has loaded. You won't get any A's. You'll need to wrap this in document.ready or something similar.
$(document).ready(function(){
//put your script block here.
});
2). You need to encodeUriComponent on this.href.
3). You seem to be assuming it is a fully-qualified URL, never a relative URL.
4). If what you're lookng for is a redirect, you'll need to just use a redirect header (CURL isn't necessary):
if (array_key_exists('ref', $_GET))
{
header('Location: '.$_GET['ref']);
exit;
}
Note -- this must go ABOVE any output... basically, move your php block to the very top of the file.
Clarification. Not a redirector at all:
if (array_key_exists('ref', $_GET))
{
echo file_get_contents($_GET['ref'];
}
The above block replaces all of your PHP code in 3 easy-to-understand lines.
I'm not expert at jquery but I believe you need a ready function
$.ready(function() {
$('a').click(function(e){
e.preventDefault();
document.location.href='http://www.redirectlink.com?ref='+this.href;
return false;
});
});
Try above...
@Mayank,
Is correct,
do it like this:
<script>
$(document).ready(function(){
$('a').click(function(e){
e.preventDefault();
document.location.href='http://www.redirectlink.com?ref='+this.href;
return false;
});
});
</script>
also, shouldn't the script start as:
<script type="text/javascript">
...
</script>
You need to wrap the whole thing like:
jQuery(document).ready(function($) { [your code] });
What's more, I think that
e.preventDefault();
and
return false;
have same behavior.