Forum Moderators: coopster

Message Too Old, No Replies

Calling a function from Javascript

Calling a PHP function from Javascript

         

Kronos

4:04 pm on Jul 24, 2003 (gmt 0)

10+ Year Member



Hi,

I have to run a PHP function once a visitor leaves a web page. But, I know that PHP won't let me do that, I have to use the Unload function of JavaScript. What I'm wondering is, is it possible to call a PHP function on another page directly from a JavaScript command on the web page? Thanks!

killroy

4:13 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



php executes on your server, not at the client. Normally when the client leaves they do so by going somewhere else, and your server will NOT be notified in any way.

Most operations that need to know when the user leaves, simply use some form of timeout, say 60minutes. If for 60 minutes the same IP doesn't not request any other pages, asume he's left.

If it's a different kind of action, the only way would be to rout the user through another page first. This would very much annoy them and is not recommended.

The last possibility I can think of is to try to load an image for example that then executes the PHP function on the server. you could try to load it into an invisible item, but again I'm not sure if this will fail due to the browser unloading the page.

The last possibility is a pop up with all it's accosiated misgivings and of course issues with popup blocking software.

All in all I'd try to redesign the system not to need that last PHP call at all.

SN

patrixs

4:14 pm on Jul 24, 2003 (gmt 0)

10+ Year Member



(EDITED)

I posted right after "killroy" so I didn't see his message... I recomend this :

Most operations that need to know when the user leaves, simply use some form of timeout, say 60minutes. If for 60 minutes the same IP doesn't not request any other pages, asume he's left.

(EDITED END)

You cannot call a PHP function from javascript. PHP is service-side and Javascript is client-side.

You can do the following tho.

<body onunload="window.open('PAGE.php','goodbye','width=20,height=20');">

and the page that pops up runs the PHP function.

on the page that pops up you can add the following so once the php function is finished it closes the pop up window.

<body onload="window.close();">

People are ganna hate me for helping you with this lol (people hate when a site is spying on them)

[edited by: patrixs at 4:18 pm (utc) on July 24, 2003]

bcolflesh

4:14 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP is server side - your client side script would have to open another page on UnLoad where the PHP script could execute, then return the results to the browser.

vincevincevince

7:08 pm on Jul 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



look at this before going down that road if I were you:

[webmasterworld.com...]

GeorgeGG

12:03 am on Jul 25, 2003 (gmt 0)

10+ Year Member



Not knowing what you want to do or wind up with:

Use onunload/pop-up window, log time to server
un-php.htm
<html><head>
<title>access php</title>
</head>
<body onunload="window.open('un-php.php','o','width=1,height=1');">
This is a page.
</body></html>
////////////////

Log time
un-php.php
<?
print "<body onload=\"window.close();\">";

$hour_min_sec = getdate();
$hour = $hour_min_sec['hours'];
$min = $hour_min_sec['minutes'];
$sec = $hour_min_sec['seconds'];

$handle = fopen ("/home/georgegg/log-php/js-php.txt", "a+");
fwrite($handle, "$hour:$min:$sec\n");
fclose($handle);
?>
//////////
//////////

Access PHP getdate() function with JS
js-php.htm
<html><head>
<title>access php with js src attribute</title>
</head>
<BODY">
Set current_hour_min_sec_var to "Unknown"<br>
Show variables values<br>
<script>
// same variables as returned by .php file
var current_hour_min_sec_var = "Unknown";
document.write ("current_hour_min_sec_var = "+current_hour_min_sec_var+"<br>");
</script>
<br>

Access php file on server:<br>
<script>
document.write("<script src=\"js.php\"></"+"script>");
</script>

Should have returned hour:minute:second variable from php on server<br>
<script>
document.write ("current_hour_min_sec_var = "+current_hour_min_sec_var+"<br>");
</script>
<br>

</center>
</body></html>
////////

js.php
<?
header("Content-type: application/x-javascript\n\n");
$hour_min_sec = getdate();
$hour = $hour_min_sec['hours'];
$min = $hour_min_sec['minutes'];
$sec = $hour_min_sec['seconds'];
print "current_hour_min_sec_var = '$hour:$min:$sec';\n";
// could also log to server
?>

BTW I'm 'not' a PHP or JS person.

GGG

Kronos

12:20 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



But what if I just want to know the time that the visitor exits my site to enter it in a Database?