PHP获取当前相对于域名目录的方法,php获取当前域名
本文实例讲述了PHP获取当前相对于域名目录的方法。分享给大家供大家参考。具体如下:
http://127.0.0.1/dev/classd/index.php/download
比如这个地址,我想得到红色区域的地址用于生成站内的链接,名为 baseurl。
用 $_SERVER['SCRIPT_NAME'] 就可以了, $_SERVER['SCRIPT_NAME'] 是当前正在访问的PHP文件的相对路径,只要如下稍作处理:
从$_SERVER['SCRIPT_NAME']中获取目录部分,并且替换dirname函数可能出现的反斜杠问题
复制代码 代码如下:$baseUrl = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));
保证为空时能返回可以使用的正常值
复制代码 代码如下:$baseUrl = empty($baseUrl) ? '/' : '/'.trim($baseUrl,'/').'/';
这样就可以正常获取到 /dev/classd/ 这部分的目录地址了。
顺便看了一下 Zend Framework 的 baseUrl() 函数,结果一看那么一大堆代码就没再继续看了,我就想不通同样的功能他用了一大堆一大堆的代码,从$_SERVER里获取了一堆,然后中间经过两三个类,四五个方法才最终被我们直接调用,除了中间有三行是获取当前的PHP文件以外,其它都是没用和无谓的处理,ZF本身运行的已经够慢的了,这是什么一个意思呢。感兴趣的朋友可以研究一下。
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
