×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

writing to a table before calling a URL

writing to a table before calling a URL

writing to a table before calling a URL

(OP)
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.

RE: writing to a table before calling a URL

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.

RE: writing to a table before calling a URL

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.

RE: writing to a table before calling a URL

@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="www.gohere.com">Click it</a>


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

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
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: writing to a table before calling a URL

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 http://httpd.apache.org/docs/current/mod/mod_log_c..., 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.

RE: writing to a table before calling a URL

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 onclick event using :
  • link context menu | Open Link in New Tab
  • link context menu | Open Link in New Private Window
  • middle click
  • mouse gesture on the link ( any of the 6 link opener commands of FireGestures )
Regarding the original question, is kind of blurry. If we take it literally

Quote (sunbase)

I want to monitor who calls a URL
  • "who" - if he means logged in user, maybe not the web server's access log is the best approach
  • "a URL" - if he really means it at singular, maybe implementing routing is an overkill
sunbase, a more precise description of your goal would bring suggestions better fitting your need.

Feherke.
feherke.ga

RE: writing to a table before calling a URL

Quote (feherke)

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.

RE: writing to a table before calling a URL

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

RE: writing to a table before calling a URL

Quote:

. 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:

Quote:

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.

RE: writing to a table before calling a URL

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.

RE: writing to a table before calling a URL

Quote (OlafDoschke )

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

CODE --> php

<?php
$destname = strtolower($_GET["name"]);

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

// 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,

CODE --> 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.

RE: writing to a table before calling a URL

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.

RE: writing to a table before calling a URL

I am simply answering the post as I have read it.

This: [emboldening and punctuation added]

Quote:

... ... 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.

RE: writing to a table before calling a URL

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.

RE: writing to a table before calling a URL

(OP)
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 www.pkprops.com

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

RE: writing to a table before calling a URL

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 --> jump.php

<?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=www.pkprops.com

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.

RE: writing to a table before calling a URL

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.

CODE --> html

<a href="http://amazon.com?affid=az1234567890&prod=ASIN786543290adreff" >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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close