Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Hi
I need to insert some data into my DB when the user leaves the page through a link.
Im unable to make any changes to the links or create a redirect for this. (third party, semi encrypted script)
I can however append the links with some inline javascript, so perhaps i could use a javascript OnClick() to fire something, but Im not sure how I can get a javascript to fire php.
Anyone got any ideas?
Member
From: Loughborough, United Kingdom
Registered: 2006-04-14
Posts: 805
I've been thanked 13 times.
Offline
if i understand this right you can't change the value of the URL in the link yet you can code something to alter the structure of the html in the link?
onclick="location.href='http://www.yoursite.com/trackingscript.php?url=$urlvalue';"
your tracking script would then have to take the query sting (minus ?url=) as the value to store, otherwise your get value for url could contain a url with get values itself so it would treat these as separate variables.
then you just 301 away and you're done.
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
^^^ Now with over 400 free templates, ShoutCast, LAME + FFMPeg
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2860
I've been thanked 85 times.
Offline
Code: javascript
<script type='text/javascript'>
function UpdateDatabase() {
var update = new Image();
update.src = "name of php database updating script";
return true;
}
</script>
Code: html
<body onUnload=UpdateDatabase();>
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Thanks guys!
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
hello again, im back to this.
I was using a redirect script, but I think its causing more problems than it solves.
Haven't tried Northies above idea yet, but I think it may well be the key to this.
Just got a few questions about it.
The JS function that points to a php script. Will executing this function open the php script or will it just execute it in the background while the user goes to the links target?
the <body OnUnLoad()> execution. Might sound like a stupid question, but would it still work ok with an OnClick() in the links anchour tag?
Cheers guys, really appreciate the help here.
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2860
I've been thanked 85 times.
Offline
griffinsbridge wrote:
hello again, im back to this.
I was using a redirect script, but I think its causing more problems than it solves.
Haven't tried Northies above idea yet, but I think it may well be the key to this.
Just got a few questions about it.
The JS function that points to a php script. Will executing this function open the php script or will it just execute it in the background while the user goes to the links target?
the <body OnUnLoad()> execution. Might sound like a stupid question, but would it still work ok with an OnClick() in the links anchour tag?
Cheers guys, really appreciate the help here.
it runs silently in the background
function can be called however you like
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Cool, cheers Northie
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Lol, hello again
Slightly off topic, but its for the same thing
I need to dynamically get the directory name that the php file is in.
Ive tried
dirname($_SERVER["REQUEST_URI"]);
which gets everything from the base directory right upto the directory BEFORE the one the file is in
and Ive tried
$_SERVER['DOCUMENT_ROOT'];
Which just gets directories from where the php.ini is to the base directory.
Looked all over for this, but I know northie will have this ingrained in his mind.
Whats the function im looking for please?
Oh btw, your above javascript works perfectly mate, cheers for that!!
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2860
I've been thanked 85 times.
Offline
I would recommend something similar
you can change the -1 to various other (negative) numbers to get the various levels in your directory structure
I was hoping to have found a __DIR__ on this page, but it doesn't exist
http://uk.php.net/manual/en/language.co … efined.php
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
looks good northie, but i need it to be in multiple files in different folders at different depths and always get the right name. Think i might stick with my long way round unless it implodes.
Im doing a quick double back to that javascript.
Would I be able to but some other perameters in the call for the function, that could add a GET string to the php file in the JS function?
I really dont get JS enough to know where to start tbh.
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2860
I've been thanked 85 times.
Offline
griffinsbridge wrote:
looks good northie, but i need it to be in multiple files in different folders at different depths and always get the right name. Think i might stick with my long way round unless it implodes.
There is such a function called implode(), and usues the same syntax - delimiter,array
I use a class called deep_dir - it recursively list all the files and folders in at a given path with full names:
class
Code: php
<?php
class DeepDir{
var $dir;
var $files;
function DeepDir(){
$this->dir = '';
$this->files = array();
$this->dirFILO = new FILO();
}
function setDir( $dir ){
$this->dir = $dir;
$this->files = array();
$this->dirFILO->zero();
$this->dirFILO->push( $this->dir );
}
function load(){
while( $this->curDir = $this->dirFILO->pop() ){
$this->loadFromCurDir();
}
}
function loadFromCurDir(){
if ( $handle = @opendir( $this->curDir ) ){
while ( false !== ( $file = readdir( $handle ) ) ){
if ( $file == "." || $file == ".." ) continue;
$filePath = $this->curDir . '/' . $file;
$fileType = filetype( $filePath );
if ( $fileType == 'dir' ){
$this->dirFILO->push( $filePath );
continue;
}
$this->files[] = $filePath;
}
closedir( $handle );
}
else{
echo 'error open dir "'.$this->curDir.'"';
}
}
}
class FILO{
var $elements;
function FILO(){
$this->zero();
}
function push( $elm ){
array_push( $this->elements, $elm );
}
function pop(){
return array_pop( $this->elements );
}
function zero(){
$this->elements = array();
}
}
?>
call:
Code: php
<?
$dirName = 'where/to/look';
$dir = new DeepDir();
$dir->setDir( $dirName );
$dir->load();
foreach($dir->files as $n => $pathToFile) {
}
?>
griffinsbridge wrote:
Im doing a quick double back to that javascript.
Would I be able to but some other perameters in the call for the function, that could add a GET string to the php file in the JS function?
I really dont get JS enough to know where to start tbh.
You can call it how you like, but you will have to include the whole url, eg
[http://wwww.domain.tld/file.php?var=val&var2=val2] without the [ and ]
that way it gets treated as a page call and gets put through the php parser
Member
From: York, England
Registered: 2005-11-04
Posts: 608
I've been thanked 11 times.
Offline
Hey That class is, err... class!
Had a play n got fairly good results.
Archived it too, think i might get some use out of that
The JS thing, Im already putting a get string in with the file url anyway, but theres 1 variable thats getting created per link.
Ive got the onclick="UpdateDatabase();" in each link, but is there anyway i can extend that to include the new variable so it gets passed to the url in the function, which inturn would be passed to the file?
Not absolutely nessecary that I can do it tbh, Ive got a fairly reliable script running (with your help northie!) but Im thinking extra admin features.
Moderator
From: Yorkshire, UK
Registered: 2006-08-19
Posts: 2860
I've been thanked 85 times.
Offline
griffinsbridge wrote:
Ive got the onclick="UpdateDatabase();" in each link, but is there anyway i can extend that to include the new variable so it gets passed to the url in the function, which inturn would be passed to the file?
I don't think I understand.......
extend to include new var....?
Code: javascript
<script type='text/javascript'>
function UpdateDatabase() {
var update = new Image();
update.src = "name of php database updating script";
return true;
}
</script>
the line update.src = "name of php database updating script"; could be
Code: javascript
update.src = "http://www.domian.com/file.php?var=<?= $php_val ?>&var2=" + js_val + "&var3=val3";
If this isn't what you're looking for, get back to me
Last edited by Northie (2006-12-16 15:57:54)Member
From: Vancouver, BC
Registered: 2006-05-01
Posts: 30
I've been thanked 0 times.
Offline
griffinsbridge wrote:
Ive got the onclick="UpdateDatabase();" in each link, but is there anyway i can extend that to include the new variable so it gets passed to the url in the function, which inturn would be passed to the file?
Another alternative is to include a var in the function call itself
Code:
<script type='text/javascript'>
function UpdateDatabase(myVar) {
var update = new Image();
update.src = "http://www.mydomain.com/file.php?var=" + myVar;
return true;
}
</script>
then use
Code:
... onclick="UpdateDatabase('myVar1');" ...
... onclick="UpdateDatabase('myVar2');" ...
... onclick="UpdateDatabase('myVar3');" ...also, if you don't wish to use the onClick event handler you can use
Code:
... href="javascript:UpdateDatabase('myVar1');" ...
... href="javascript:UpdateDatabase('myVar2');" ...
... href="javascript:UpdateDatabase('myVar3');" ...| Never |


