Well I am new to this so I want to record when the user clicks on the link that php prints and query a mysql database. I know how to query the database using php already but I'm not sure if it is possible to know if the user clicked on the link.
I printed a link like so.
print ('<a id="myLink" href="http://www.google.com" target="_blank">google</a>');
To track the link, you'd need to create a link tracking script on your server. i.e. linktracker.php
Then, change your code to point the link to that script, passing the forwarding url i.e
<a id="myLink" href="http://mysite.com/linktracker.php?url=http://www.google.com" target="_blank">google</a>
In linktracker.php, you would need something like:
<?php
$url = $_GET['url'];
// update your database click count for the url
// i.e UPDATE linkclicks SET clickcount = clickcount + 1 WHERE url = '$url'
// forward the user to the end location
header("Location: $url");
You need to build a URL redirection mechanism.
$link = 'http://www.google.com';
echo '<a href="/redir.php?target="'.encodeUriComponent($link).'>google</a>';
then make a redir.php:
<?php
$targetUrl = $_REQUEST['target'];
// log this targetUrl to your MySQL database.
header( 'Location:'.$targetUrl);
I absolutely wouldn't do this in JavaScript if you want to try to track links shares or something of that nature.
add onclick="handleClick()" and write javascript function named handleClick to report the click to the server uaing ajax
Unless the link is to your own site, you'll need to use a client-side scripting language such as JavaScript (could utilise jQuery too) to send the user's click event back to the server.
Related
I want any other server request goes through my server just like link in Gmail and i checked it there data-saferedirecturl="".
So how to use data-saferedirecturl in PHP automatcally in our website?
Exapmple:
Link
All links in Gmail are interpreted in the browser.
The data-saferedirecturl tag is added automatically.
So the href shows the link that you will be clicking in the bottom of your browser but sends you to a google-originated URL like https://www.google.com/url?hl=en-GB&site.com/324dd3.
This way the third party don't have access to sensitive data.
There are different ways to achieve this.
You can point all the redirects to the same page with the safe URL as a GET parameter (remember to use urlencode):
Link
Then in handler.php something like:
$desturl = isset($_GET["safeurl"]) ? $_GET["safeurl"] : false;
if($desturl != false){
//do something
header("location: ".$desturl);
}
If you want to use data-saferedirecturl you have to use some JavaScript, and intercat somehow with your server (ex. ajax calls).
Example (using jQuery):
HTML:
Link
JavaScript:
$(".test").click(function(e){
e.preventDefault();
let url = $(this).data("saferedirecturl");
// do ajax or check somehow the URL
location.href = url;
})
I have an affiliate program and have a serious problem that I can figure out.
My affiliates have links like this...
http://example.net/?p=14&ref=delta88
Once the page loads it changes to...
http://example.net/?p=14
Which totally gets rid of the ref id. I need it to keep the whole URL in the bar in case they hit refresh. Because when you hit refresh it takes the affiliate out of the system and just let's people join without an affiliate.
The way my code works for the pages is this...
That URL goes to an index.php file. In that file it finds all the affiliates information. It then uses an include to show the page. So it's not pointing directly to the page. I need to use the include because I store about 27 pieces of data in strings and I can't put that information in a URL as queries and have it forward to that page.
I added that information because it may be because of the include that's causing it and that will help you better figure out a solution for me.
Use a SESSION, its like a variable that holds for each user, here is a tutorial but works like:
<?php
session_start();
if(isset($_GET["ref"]){
$_SESSION["ref"] = $_GET["ref"];
}
?>
Now, in any PHP that open the user, will have that variable set ( $_SESSION["ref"])
you can keep current url in variable , see below used actual_link to hold the current url data.
if($_GET){
$actual_link = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//if you want to redirect page on same url just call header with $actual_url
header('Location: '. $actual_link);
}
Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);
Is there any way in php to have a link so that when the user clicks on the link, they call a function and then immediately after calling the function, the user is redirected to another webpage.
$acceptEmailInvite; //append this url to the url in the link
if($app[UID]) {
//how do I have a link connect to two redirects?
$acceptEmailInvite = '/idx.php/store/accept/'.{$org['id']}; //link to this first
$acceptEmailInvite = 'getLink?org='.$D['bta']; //then link to this
}
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
EDIT: I meant that these two things happen only when the link is clicked. So the user should not be redirected unless the link is clicked. Sorry for the confusion.
Of course.
<?php
foo();
header("Location: http://example.com/");
exit();
The simplest way to do this is to have a page that performs the action, then redirects the user. For example:
/idx.php/organization/accept/
<?php
// Get your ID's, presumably from the URL
// Accept the invitation
acceptinvite($org['id']);
// Use header to redirect the user when you are done
header("Location: getBuzzed?org=".$D['betacode']);
exit();
?>
Your link would look like this:
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
.. but the user would see theirself go directly to getBuzzed....
You can see this functionality in action at Google or Facebook. If you google StackOverflow.com, the link in the results actually points here:
http://www.google.com/url?sa=t&rct=j&q=stackoverflow&source=web&cd=1&cad=rja&sqi=2&ved=0CCsQFjAA&url=http%3A%2F%2Fstackoverflow.com%2F&ei=ii-dUKaiMc_Psgad8oGYBA&usg=AFQjCNERidL9Hb6OvGW93_Y6MRj3aTdMVA
Google uses this page to track who has clicked on the link, and alert you if this link is dangerous.
Important
The page with the header command must not echo anything. If it shows any content, then you will get an error message when you try to use the header command.
How can I get the id as a variable from that statement in same page?
I need to capture the #dialog to display in a popup and can't do that if I send the id to a different page.
Link
I've been using this to get it from the link but I need to capture it in same page. This is what i was using :
$img = isset($_GET['id'])
Everything after the Hash-Sign is evaluated by the browser only and not sent to the server. You therefore cannot access it.
Maybe you meant:
?id=<?php echo $image['image_id']; ?>#dialog
Otherwise you would have to use JS.