Forum Moderators: coopster
example web page:
<html>
<head>
<script>
webbug = new Image();
</script>
</head>
<body>
<form>
<input name=button1 type=button onclick="javascript:webbug.src=pix.php?variable=button1">
</form>
</body>
</html>
So now clicking the button (if I rembered all the quotes and parens right :) ) will cause the server to deliver up the file pix.php which can then excecute code:
For example,
<?
session_start();
$_SESSION['variable']=$_GET['variable'];
?>
I often use something like pix.php to insert things into a database I want to keep track of.
Have fun.
Also, I don't know if anyone else uses them exactly the way I outlined. By changing the src with JavaScript you force the browser to download the file without displaying anything. This is better because some privacy programs and browser settings screen out traditional tracking pixels.
It's worse because it relies on the user's browser having JavaScript active, but you already are since you specificed an onClick handler.
Play around with the code I wrote. Remember as with other JavaScript tasks it is probably better to call a function with the onClick than do it inline if it gets any more complicated.
First of all, I presume that pix.php is a separate file/script that goes into the same directory as the main html/php file? I placed the following (your's basically) code in it:
<?php
session_start()?
S_SESSION['variable']=S_GET['variable'];
?>
Next, I created a file named webbug1.php with the first part of your code essentially verbatim:
<html> <head> <script>
webbug = new Image();
</script> </head> <body>
<form>
<input name="button1" type="button"
onSubmit = "javascript:webbug.src=pix.php?variable=button1">
</form> </body> </html>
I found I got an error if I used "onClick", but not with "onSubmit". I am a little confused about what is going on in the javascript code in the "onClick" part of the input line. Assuming I am anywhere near the right track with this approach, may I ask how I would proceed if I were to attempt to pass a value "magnet" to a PHP variable $action in the main script? Thanks for your patience.
My JavaScript was probably buggy. Calling JS from event handlers usually works a lot better if you call a function instead of doing it inline.
Anyway, I wrote and tested a little scrip that works. I also uploaded it a server on the net. If you want to see it, sticky me I'll give you the address.
Here's three files.
1) bug.html with two buttons
2) pix.php the bug that sets a php session variable
3) result.php lets you see the php variable
To test:
1) open both bug.html and result.php in different browser windows. Make sure they are going through a webserver with php installed instead of just through the filesystem.
2) click a button in bug.html
3) reload result.php
bug.html
==================
<html>
<head>
<script>
webbug = new Image();
function clk(param) {
iURL="pix.php?variable=" + param;
webbug.src=iURL;
}
</script>
</head>
<body>
<h1>Test Bug</h1>
<form>
<input name=button1 value=button1 type=button onclick="clk('button1');">
<input name=button2 value=button2 type=button onclick="clk('button2');">
</form>
</body>
</html>
pix.php
==========================
<?
session_start();
$_SESSION['variable']=$_GET['variable'];
?>
result.php
==========================
<?
session_start();
echo "<h1>Results</h1>";
echo "variable is " . $_SESSION['variable'];
?>
Results
variable is
Despite various manners of refreshing both pages and clicking the buttons in the "bug" page and, I could not output a value for "variable" in the result.php page, presumably either "button1" or "button2" as the case may be. Looking at your code, it seems fine. I could not, by several means, extract a value from $_SESSION['variable'], so maybe there is a hitch somewhere. I will play with it further tonight.
I think I'll send a note via sticky to have a look at your uploaded files behavior.