由於使用Javascript (通常情況下)用戶端的技術,和PHP (通常情況下)伺服器端技術,自從HTTP是一種“無國籍”協議,兩種語言不能直接共用變數。
然而,人們可能通過變數兩者之間的關係。途徑之一是完成這一創造Javascript代碼用PHP ,並重新整理瀏覽器本身,通過特定變數返回的PHP指令碼。下面的例子恰恰說明如何做到這一點-它使P HP代碼捕獲螢幕的高度和寬度,這通常是唯一可能在用戶端。htmlspecialchars()函數
<?php
echo "<input type='hidden' value='" . htmlspecialchars($data) . "' />n";
?>
php url
<?php
echo "<a href='" . htmlspecialchars("/nextpage.php?stage=23&data=" .
urlencode($data)) . "'>n";
?>
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />n";
echo "Screen height is: ". $_GET['height'] ."<br />n";
} else {
// pass the geometry variables
// (preserve the original query string
// -- post variables will need to handled differently)
echo "<script language='javascript'>n";
echo " location.href="${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=" + screen.width + "&height=" + screen.height;n";
echo "</script>n";
exit();
}
?>