简体   繁体   English

PHP通过URL传递参数

[英]PHP passing parameters via URL

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。

You should use .htaccess 您应该使用.htaccess

Here's an example: 这是一个例子:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [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! 干杯!

Your third example is an example of how REST identifies a server side resource. 第三个示例是REST如何识别服务器端资源的示例。 What you are talking about here sounds very much like REST will do what you want. 您在这里所说的听起来很像REST可以完成您想要的事情。 I would suggest starting here (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services). 我建议从这里开始(http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services)。

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

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