PHP 通过 URL 传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3901635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP passing parameters via URL
提问by sikas
I want to know how I can pass parameters between pages through the URL without having to add variables eg:
我想知道如何通过 URL 在页面之间传递参数而无需添加变量,例如:
mydomain.com/file.php?var1=val1&var2=val2&...varN=valN
I want to use it as follows:
我想按如下方式使用它:
mydomain.com/file.php?val1-val2-...-valN
I also see in some website the URL is in the following format
我也在一些网站上看到 URL 的格式如下
mydomain.com/file/####
which redirects to another page without changing the URL as if this is the URL to the file.
重定向到另一个页面而不更改 URL,就好像这是文件的 URL。
回答by Claudiu
You should use .htaccess
你应该使用 .htaccess
Here's an example:
下面是一个例子:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=&var2= [NC,L]
This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.
这基本上意味着,如果 URL 是根据上面的正则表达式(数字 - 斜线 - 字母数字、破折号或下划线)格式化的,那么应该从 index.php 显示内容,同时传递两个名为 var1 和 var2 的参数,var1 具有数字的值,第二个具有第一个斜杠后面的值。
Example:
例子:
mysite.com/20/this_is_a_new_article/
Would actually mean
实际上意味着
mysite.com?var1=20&var2=this_is_a_new_article
Of course, in your index.php file you can simply take the values using
当然,在您的 index.php 文件中,您可以简单地使用这些值
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
Cheers!
干杯!
回答by Codemwnci
Your third example is an example of how REST identifies a server side resource. What you are talking about here sounds very much like REST will do what you want. I would suggest starting here (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services).
您的第三个示例是 REST 如何识别服务器端资源的示例。你在这里谈论的听起来很像 REST 会做你想做的事。我建议从这里开始(http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services)。