将javascript放入PHP中

Put javascript inside PHP

本文关键字:PHP 放入 javascript      更新时间:2023-09-26

我尝试将document.URL放入PHP代码中,如下命令所示:

> $string=explode('/',document.URL)

我确信它不会起作用,但有什么办法吗?我是新来的!

我们可以做一些事情,比如制作一个函数,从http://webcheatsheet.com/php/get_current_page_url.php):

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

那么我们可以:

$string=explode('/',curPageUrl());

您不能执行JavaScript来返回页面的URL,因为php是在页面加载到服务器上之前运行的(这就是为什么它被称为服务器端语言)。即使可以,最好使用在中执行语句的语言。

Php在服务器上运行。Javascript在浏览器中运行。它们是两个不同的位置。因此,您不能将php与javascript 混合使用

你可以试试这个

//Simply call the getCurrentURL() function to get the URL
$string=explode('/', getCurrentURL())
//This function forms the URL you see in your browser
function getCurrentURL()
{
    $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    $currentURL .= $_SERVER["SERVER_NAME"];
    if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
    {
        $currentURL .= ":".$_SERVER["SERVER_PORT"];
    } 
        $currentURL .= $_SERVER["REQUEST_URI"];
    return $currentURL;
}