PHP与JS中URL路径的问题
1 URL地址或者资源地址可以通过JS获取,存入COOKIE或SESSION中,有效期为浏览器进程。例:createBaseUrl这个方法输出的是截至项目名之前的URL(包含项目名)
加入www.123.com/app/123.html,那么输出的就是www.123.com/app。
下面是完整的函数:
function createBaseUrl(){
//Begin to get the url dynamically
var basePathLength = window.location.pathname.length;
var baseUrl = window.location.pathname.substring(1);
baseUrl = baseUrl.substring(0,baseUrl.indexOf('/'));
baseUrl = window.location.protocol+"//"+window.location.host+":"+window.location.port+" /"+baseUrl;
//End to get the url dynamically
return baseUrl;
}
window.location.href = createBaseUrl()+"/board.php";
var baseUrl = createBaseUrl();
$.cookie('baseUrl',baseUrl,{path:'/'});
var basePath = window.location.pathname.substring(1);
basePath = basePath.substring(0,basePath.indexOf('/')+1);
$.cookie('basePath',basePath,{path:'/'});
PHP中包含文件路径问题很麻烦,这样可解决:
$basePath = $_COOKIE["basePath"];
$path = $_SERVER['DOCUMENT_ROOT'].'/'.$basePath;
include $path.'dao/board_dao.php';
页:
[1]