Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to a table before calling a URL

Status
Not open for further replies.

sunbase

Technical User
Jun 18, 2009
15
0
0
GB
I want to monitor who calls a URL and when so I want the click on the link to write to a table before executing the link is this possible and how does a simpleton like me make it happen? Please keep it simple if possible.
 
Clicking a link causes a request to the webserver, and in general it does not care by whom, but there are several possible ways to configure and to limit access. All of them are a topic for whatever webserver you use and not PHP.

There is a pattern to use in PHP, where all urls are not directly accessing any file, but go through a routing.php to find a controller or through a single controller, if a site does not need a variety of controllers. And a controller.php might first check whether authentication/login has already been passed and is remembere in a current PHP $_SESSION.

A very general access permission check -eg also on html,jpg, pdf, and any other non PHP files - can only be done before PHP becomes active, and is rather a question about your webserver, though.

Bye, Olaf.
 
Just route the link through a secondary URL (aka a 'jump' script) that collects the data then redirects to the end target. This is how "URL shortening" operates.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
@sunbase,

This can be easily done by calling a JS method off your anchor/link using simple onclick() method.

Say for instance:
Code:
<a onclick="storeIT('link id or whatever you want to store');" href="[URL unfurl="true"]www.gohere.com">Click[/URL] it</a>


<script>
function storeIT(strVal) {
[indent]$.ajax({
        url: "storeIT.php",
        type: "POST",
        data: 'strVal='+escape(strVal),
        success: function(response) {
// whatever you may want to do once process finishes ...
            }
        });[/indent]
}

Please note that I use JQuery and so, you will need to make sure you're loading the framework and that is beyond the scope of this post. That being said, same could be done using plain JS.




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
1. Routing will only work for urls triggering some url shortener/expander jump script or mod_rewrite and not direct urls to some file (htm,pdf,jpg,...)
Therefore the basic thing would be mod_rewrite (in case of Apache webserver)
2. JS binding to onclick events only is about literally clicking links. If you want to know user,uri about each http request this is again about the webserver. In general there is an access log in a logs folder (again rather Apache), and to have user information in it you have to specify a LogFormat with %u, see but this will only be set after authetication and not after your own PHP login authentication but after http authentications as specified/restricted via .htaccess

So, do you see why I say this is rather a web server topic in the first place? All this stuff goes back to apache, first. To be able to tackle this with PHP some requirements have to be fulfilled in the first place, you have to plan an URL schema which makes every url, even those for non php files be routed by mod_rewrite to some php script processing the request, which is called routing. Otherwise the normal file extension pass by any PHP script and are only processed by guess what? The web server.

Bye, Olaf.
 
Hi

SouthBeach, please note that links can be followed by other actions than clicking. In my FireFox 48.0 I can open the link on my test page without triggering the [tt]onclick[/tt] event using :
[ul]
[li]link context menu | Open Link in New Tab[/li]
[li]link context menu | Open Link in New Private Window[/li]
[li]middle click[/li]
[li]mouse gesture on the link ( any of the 6 link opener commands of FireGestures )[/li]
[/ul]

Regarding the original question, is kind of blurry. If we take it literally
sunbase said:
I want to monitor who calls a URL
[ul]
[li]"who" - if he means logged in user, maybe not the web server's access log is the best approach[/li]
[li]"a URL" - if he really means it at singular, maybe implementing routing is an overkill[/li]
[/ul]

sunbase, a more precise description of your goal would bring suggestions better fitting your need.


Feherke.
feherke.ga
 
feherke said:
if he means logged in user...

That's why I said user ... "after http authentications", not after his own authentication/login, the webserver has no idea about php $_SESSION variables, for example.
You may associate ip address to logged in user, live or as aftermath. The web server access log has the big advantage it captures any access to anything. That's why tools for web site traffic statistics simply evaluate the logs. You may even have something preinstalled.

If you want to analyse web site traffic, another state of the art approach is outsourcing that, eg to google analytics.

Bye, Olaf.
 
Hi

Yes, that was my first reaction too when initially read the question. ( Even wrote half answer very similar to yours, just delayed it then you posted in meantime. Doh, now I see you did it today too. [mad] ) But after rereading the question I feel the OP not needs a generic solution.

Feherke.
feherke.ga
 
. Routing will only work for urls triggering some url shortener/expander jump script or mod_rewrite and not direct urls to some file (htm,pdf,jpg,...)

Course it will, ... A link, is a link, is a link, regardless of what happens to be in the position of anchor text, and covers the requirement stated in the first post:

I want to monitor who calls a URL and when so I want the click on the link to write to a table

Now IF "I want to monitor who calls a URL" actually means "I want to see the incoming referrals", that is already in the server access logs, and does not require anything additional.




Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Well, you only think about the links in a browser DOM. I don't even consider this, as some request can also be made without any browser and link involved. The question was about a URL, not a link. I would tackle this at the server side only.

Bye, Olaf.
 
OlafDoschke said:
Well, you only think about the links in a browser DOM.

No, because that would make it a javascript question and solution.

It's a really simple concept;

This is the basic PHP code I have used on sites for tracking affiliate links.
Also in attached file
PHP:
<?php
$destname = strtolower($_GET["name"]);

$Dests = array(
	"amazon"=>"[URL unfurl="true"]http://www.amazon.co.uk",[/URL]
	"ebay"=>"[URL unfurl="true"]http://www.ebay.co.uk",[/URL]
	"clickbank"=>"[URL unfurl="true"]http://affname.reseller.hop.clickbank.net",[/URL]
	"auctads"=>"[URL unfurl="true"]http://www.auctionads.com/",[/URL]
	"cj"=>"[URL unfurl="true"]http://www.cj.com/",[/URL]
	"yahoo"=>"[URL unfurl="true"]http://www.yahoo.com/",[/URL]
	"goog"=>"[URL unfurl="true"]http://www.google.com/",[/URL]
	"msn"=>"[URL unfurl="true"]http://www.msn.com/"[/URL]
	);

// Code can be inserted in here to log the clickthrough into a database ;

header("HTTP/1.1 301 Moved Permanently");
// comment out the line above to send the default 302 redirect;
header("Location: " . $Dests[$destname]);
exit();

?>

The code above goes in a document local called jump-2-uri.php (for example).

And the link needs to be one with a single parameter,

HTML:
<a href="local-jump-2-uri.php?name=amazon" >anchor text</a>

Then when that link is clicked, the user agent requests the 'jump-uri' the code reads the parameter (amazon) matches it against the associative array keys and then redirects to the target URL (amazon.co.uk) in the array values.

To extend it is also easy, the assoc array moves into a database and instead of a 'name' you replace it with an ID that is produced when you add the entry, and hey presto! You have the basis of a short URL system.

As I said a couple of posts back tracking referrals is only a matter of checking access logs but if you want to do that with PHP it is also possible.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Okay, that's a solution for outbound/external links, I don't get the question as asking about external links, specifically. But you assume you have control about the links target URL.

Well, sunbase, you see your question raises many interpretations.

I can even come up with another one, assumed you want to know where employees surf the web, then you neither have control about the links themselves and could route them through some php, nor you detect the URLs visited on your sites webserver, as you don't host all the internet. The point of interest then is your endpoint, router.

There are many different topics around this question, so please rephrase what you want to count more specific. Traffic on your web site? Traffic to one certain link? Traffic to external sites off your site? Traffic to whatever sites from your office employees? There is no single point you can act on any of these URLs to count their usage.

Bye, Olaf.
 
I am simply answering the post as I have read it.

This: [emboldening and punctuation added]
... ... and when so I want the click on the link to write to a table, before executing the link, is this possible and how does a simpleton like me make it happen?

[edit after pressing wrong button]
Suggest the following to me; When a visitor on my web site clicks a link, I want to know about it.

So that is the question I have answered. A philosophical discussion about what the TS actually meant serves no purpose as neither of us can actually know, so for the moment I shall remain agnostic as to why this thread exists until further clarification to that end is presented.




Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.


Never mind these jesus character, stars had to die for me to live.
 
I understand this, Chris, I have said I understand this. Your don't have to defend your answer, I was just stating to you I have a different interpretation.

I like your answer in the way it can even count links to external sites. You lack that count when only processing the access_log count. On the other side your way of counting link usage don't count the landing hits on the site. You can combine counting incoming and staying traffic with mod_rewrite routing every url for your website to a single url (script) counting the requests and provide links to external sites using your solution.

I still think it makes sense to ask sunbase for clarification and to provide the reasons and open his eyes for the different interpretations his question already had, even though he's explicitly stated wanting an answer for "a simpleton like me". It just shows one thing: The different interpretations have different solutions. And isn't that fine? Sunbase may also simply pick what he needs from the ideas already presented.

Bye, Olaf.
 
Thank you all so much for your interest in and help with my little problem - just to explain more all I wanted is to keep a record of visits to a certain page by my colleagues on beahlf of client and when - mainly so I can demonstrate to the client what we are doing on their behalf sort of:

Date 2016-09-09 09:00:00 User David Client Mr Jones Site pkprops.com

then open the URL
As it happens I have sorted it with an intermeidate page that runs the query to post then redirects to the URL - crude but it works.

As ever many thanks for all the input
 
In that case you're in control about the target (href) of the original link and can use Chris local-jump-2-uri.php solution. The redirect then happens via Location header server side. Instead of his script with alias names of certain URLs you might also make a very generic one:

Code:
<?php
$desturi = strtolower($_GET["url"]);

// Code can be inserted in here to log the clickthrough (current user, url) into a database ;

header("HTTP/1.1 301 Moved Permanently");
// comment out the line above to send the default 302 redirect;
header("Location: " . $desturi);
exit();
?>

Then specify a call to jump.php?url=
Chris concept with an alias name has several advantages, though: Complex URLs with parameters in them are problematic as parameter value, because a url might contain ? and &, a parameter value can't. Also the html and browser status bar message already reveal the final URL and someone might circumvent the count, though that's maybe not much of a pain, as after leaving off once to the external site, a user might add this to favorites and from then on directly go there, you can't hinder that anyway.

Bye, Olaf.
 
So basically you are running what is, or used to be known in the affiliate marketplace as "URL masking". Which is what my script was intended for.

If you replace the "name" parameter in the anchor element href, with something like "affid" you will probably recognise the resultant URL as being an affiliate click-tracking one.
For example.
HTML:
<a href="[URL unfurl="true"]http://amazon.com?affid=az1234567890&prod=ASIN786543290adreff"[/URL] >Buy this book on Amazon</a>
so Amazon know what product to show the user and what affiliate account to credit any sale to.

The purpose of the script was to prevent the 'savvy' Internet user and possibly malicious ones removing the affiliate ID from the URL, thus bypassing 'your' commission, it also made content "stealing" and replacing the affiliate IDs somewhat more difficult for the 'lazy' affiliate types and the "crash and burn" 'marketers' who preferred to steal sites or 'pages', rather than develop their own.

This method is also somewhat more "Search friendly" and 'User friendly', simply because it keeps the link target URLs on the local domain rather than going 'off-site' to tinyurl.com, bit.ly or adf.ly

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top