简体   繁体   English

基于URL构建PHP页面

[英]Build PHP page based on url

If you have a url such as the one as follows: 如果您的网址如下所示:

http://www.example.com/400x200

is it possible to create a page which echos out 400x200 when the user visits that url using php? 是否可以创建一个页面,当用户使用php访问该URL时回显400x200?

I know how to echo the path - that is easy enough ( ltrim($_SERVER['PATH_INFO'], '/') ), but do not know how to create the page dynamically. 我知道如何回显路径-这很容易( ltrim($_SERVER['PATH_INFO'], '/') ),但是不知道如何动态创建页面。

Any help would be much appreciated, thanks in advance 任何帮助将不胜感激,在此先感谢

You may want to look at Apache Rewrites for rewriting your URL: 您可能需要查看Apache Rewrites来重写URL:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Do not know what do You mean by creating the page dynamically but I guess that using of mod_rewrite is what You need. 不知道通过动态创建页面是什么意思,但是我想您需要使用mod_rewrite

In Your .htaccess file You have to create some rules that will rewrite the URL to something distinguishable by Your PHP script - like from URL 在您的.htaccess文件中,您必须创建一些规则,将URL重写为PHP脚本可区分的内容(例如,来自URL)

http://www.example.com/400x200

to get 要得到

http://www.example.com/index.php?param=400x200

And then You can in Your index.php script do echo $_GET['param']; 然后,您可以在index.php脚本中echo $_GET['param']; ... ...

Google something about PHP and mod_rewrite : http://www.google.com/#q=PHP+mod_rewrite Google关于PHP和mod_rewrite一些知识: http : //www.google.com/#q=PHP+mod_rewrite

Assuming you're using Apache, this can be done using something called URL rewriting . 假设您使用的是Apache,则可以使用称为URL rewriting的方法来完成。 Create a file called .htaccess in your document root, and add this: 在文档根目录中创建一个名为.htaccess的文件,并添加以下内容:

# Turn URL rewriting on
RewriteEngine on
Options +FollowSymlinks

# Rewrite rule
RewriteRule ^(\d+x\d+)/?$ index.php?dimensions=$1 [L]

The first two lines turn the rewrite engine on, and the third line defines a RewriteRule . 前两行打开重写引擎,第三行定义RewriteRule The first part ^(\\d+x\\d+)/?$ is a regular expression that the part of the URL after the domain will be matched against. 第一部分^(\\d+x\\d+)/?$是一个正则表达式,将与该域名之后的URL部分匹配。

The second part index.php?dimensions=$1 is the URI that will be rewritten to. 第二部分index.php?dimensions=$1是将被重写的URI。 The client doesn't see this, but PHP will. 客户端看不到,但是PHP会看到。

If I do a print_r($_GET) in index.php with the URL http://localhost/400x300 , I get this: 如果我在index.php使用URL http://localhost/400x300进行print_r($_GET)http://localhost/400x300得到以下信息:

Array ( [dimensions] => 400x300 )

This is from the standard $_GET superglobal array in PHP and can be used as normal. 这来自PHP中的标准$_GET超全局数组,可以照常使用。 URL rewriting leaves the URL as it is in the browser, yet allows you to turn it into one usable by PHP with a query string. URL重写保留了浏览器中的URL,但允许您使用查询字符串将其转换为PHP可用的URL。


To make your script a bit easier to use, you could split the expression up to get separate X and Y values: 为了使脚本更易于使用,您可以将表达式拆分成单独的X和Y值:

RewriteRule ^(\d+)x(\d+)/?$ index.php?x=$1&y=$2 [L]

Which will give an array like this: 这将给出这样的数组:

Array ( [x] => 400, [y] => 300 )

The request URI ( /400x200 ) is stored in the server superglobal: $_SERVER["REQUEST_URI"] . 请求URI( /400x200 )存储在服务器的全局变量$_SERVER["REQUEST_URI"]

You need to take that and route the URI accordingly. 您需要这样做并相应地路由URI。 The simplest possible scenario: in your index.php, place this code: 可能的最简单方案:在index.php中,放置以下代码:

$uri = trim($_SERVER["REQUEST_URI"],"/");

if (preg_match("/\d+x\d/")) {
    list($width,$height) = explode("x",$uri);
    // some logic with the above vars, e.g. include a view script
}

What this does is check whether the URI has the format {number}x{number} , extracts both numbers and stores them in the variables $width and $height . 这是检查URI是否具有{number}x{number}格式,提取两个数字并将它们存储在变量$width$height You can then do whatever you like with the variables. 然后,您可以对变量进行任何操作。

In order to make the request always point to the file containing this code, edit your .htaccess and put in something like this: 为了使请求始终指向包含此代码的文件,请编辑.htaccess并输入如下内容:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

(the .htaccess code is copied from the default Zend Framework project, in case anyone asks). (如果有人询问,则从默认的Zend Framework项目复制.htaccess代码)。

Make it a GET variable like 使它像GET变量

http://www.example.com?size=400x200 

Then you can retrieve the String with 然后您可以使用

$size = $_GET['size'];

What I'd recommend you to do is to get the values based on split or explode() 我建议您要做的是基于split或explode()获取值

$lw = $size.explode('x',$size);
$length = $lw[0];
$width = $lw[1];
//Manipulate the values accordingly like
echo $length.'x'.$width;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM