PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
TD Pic of Day PHP - Run your own picture of the day script from any site anywhere with this handy script


[back]
WB01624_.gif (281 bytes) 01/22/00 "Using JavaScript with PHP" WB01626_.gif (272 bytes)[next]

Using JavaScript with PHP

There will be times when you need to use other languages than PHP to get a desired effect on the client side. JavaScript can be quite useful for realtime activities involving the browser, playing sounds and manipulating graphics with the mouse or keyboard. The downside of JavaScript is clients/surfers can somewhat easily turn off or disable javascript with their browser and potentially render your script inoperable. Ah, but there are workarounds for this!

In my card game, Guts or Glory, I needed a simple way (and also a more complex way) to prevent surfers from going backwards with their browser. If you have played the game before you will understand why allowing surfers to use the back button would not work. I did it at the PHP script level by writing the changes to a file each time the submit form is pressed, but I wanted to add an additional element of protection by eliminating the forward and backward buttons on the browser, as well as the URL locator. This is actually pretty basic JavaScript but I'll share the code with you here how to pop a window from within an HTML or PHP enabled page.

<form name="pop_window">
<input type="button" value="Launch PHP Script In New Window">
onClick="window_name=window.open('http://www.php-scripts.com/php_diary/example26.php3','window_name', toolbar=no,status=no,width=300,height=150');">
</form>

The URL to the page or script you will be launching goes in the first spot, the name of the window second and then the options after that.The value options for toolbar and status are simply yes or no. yes would display the item, and no won't. The width and height control the size of the window you want to create.

Now we need some code to fit in the window we just created. When you want to force something like this on the client, for lack of a better expression, then you need to make sure they simply don't directly call the script from a full browser window and defeat the purpose of a javascript created smaller window without toolbar and status bar. You can do this using PHP, of course, and by checking the $HTTP_REFERER variable and if it isn't from the page you designate than you ship them back there to come via that way using the header function. That way you force them to come via the javascript pop box and no other way. Here's the code to do this:

<?
if($HTTP_REFERER != "http://www.php-scripts.com/php_diary/012200.php3")

{
  header("Location: http://www.php-scripts.com/php_diary/012200.php3");
}
?>

<HTML>
<BODY>
<center>You came from the right place, thank you!
</BODY>
</HTML>

Example #26:

Now to adequately test this example, type the same link to the example script directly into your browser and you'll see you quickly get bounced back here. Remember when you type anything into the URL locator window the $HTTP_REFERER variable will show up as nothing "", and thus does not equal the exact url of this page. I used this technique a month ago to prevent offsite script calls using the rating script and correctly identify the page name (12/22/99). If you would like to learn more about JavaScripts visit http://www.tdscripts.com/. I just released a freeware JavaScript countdown script yesterday there.

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 4.05 (1303)

[back]WB01624_.gif (281 bytes) 01/22/00 "Using JavaScript with PHP" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 02/15/00 10:08