简体   繁体   English

PHP的URL参数

[英]URL parameters with PHP

Are there ways to pass variables in a URL similarly to GET data? 有没有办法像GET数据一样在URL中传递变量? For example, with slashes? 例如,带有斜线?

I currently have a single .php file which reloads a div with different content using javascript to navigate pages, but as you can imagine, the address in the address bar stays the same. 我目前只有一个.php文件,该文件使用javascript导航页面重新加载了具有不同内容的div,但是可以想象,地址栏中的地址保持不变。

I want users to be able to link to different pages, but that isn't possible conventionally if there is only one file being viewed. 我希望用户能够链接到不同的页面,但是按惯例,如果仅查看一个文件,那是不可能的。

You're probably going to want to use something along the lines of Apache's mod_rewrite functionality. 您可能想要使用类似Apache的mod_rewrite功能的东西。

This page has a nice example http://www.dynamicdrive.com/forums/showthread.php?51923-Pretty-URLs-with-basic-mod_rewrite-and-powerful-options-in-PHP 该页面有一个漂亮的示例http://www.dynamicdrive.com/forums/showthread.php?51923-Pretty-URLs-with-basic-mod_rewrite-and-powerful-options-in-PHP

Try using: 尝试使用:

$_SERVER['QUERY_STRING']; // Or
$_SERVER['PHP_SELF'];

If that doesn't help, post an example of what kind of URL you are trying to accomplish. 如果这样做没有帮助,请提供您要完成的URL类型的示例。

Something like this might do the trick; 这样的事情可能会解决问题。

place this in /yourdir/ 将此放在/ yourdir /

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ yourindexfile.php?string=$1 [QSA,L]

All requests will be sent to yourindexfile.php via the URL. 所有请求将通过URL发送到yourindexfile.php So http://localhost/yourdir/levelone becomes yourindexfile.php?string=levelone . 因此, http://localhost/yourdir/levelone成为yourindexfile.php?string=levelone

You'll be able to break down the string like so; 您将可以像这样分解字符串。

$query= explode('/', rtrim($_GET['string'], '/'));

the technology your looking for is .htaccess . 您寻找的技术是.htaccess technically this isn't possible, so you'll have to hack your mod rewrite to accomplish this. 从技术上讲这是不可能的,因此您必须修改您的mod重写才能完成此任务。

  RewriteRule On +FollowSymLinks
  RewriteCond %{SCRIPT_FILENAME} !-d  
  RewriteCond %{SCRIPT_FILENAME} !-f 
  RewriteRule ^(user)/([^\.]+)$ ./index.html?tab=user&name=$2

add this to your .htaccess page in your top directory. 将此添加到顶级目录中的.htaccess页面。 you'll have to alter your website structure a little bit. 您将不得不稍微更改您的网站结构。 assuming that index.html is your index. 假设index.html是您的索引。 this is a backwards rewrite so if one was to go to the page with the query string it won't redirect them to the former page and if one went to the page without the query string it will work like GET data still. 这是一种向后重写,因此,如果要使用查询字符串转到该页面,则不会将它们重定向到前一页;而如果没有查询字符串转到该页面,则它将仍然像GET数据一样工作。

you GET this data with your php file using $_GET['tab'] and $_GET['name'] 您可以使用$_GET['tab']$_GET['name']通过php文件获取此数据

我认为Symfony Routing组件正是您所需要的;)可用作独立组件,可为类固醇提供动力。

I'm doing it like this (in my like framework, which is a fork of the JREAM framework ): 我正在这样做(在我的类似框架中,这是JREAM框架分支 ):

RewriteEngine On

#When using the script within a subfolder, put this path here, like /mysubfolder/
RewriteBase /mysubfolder/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Then split the different URL segments: 然后拆分不同的URL段:

$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url_array = explode('/', $url);

Now $url_array[0] usually defines your controller, $url_array[1] defines your action, $url_array[2] is the first paramter, $url_array[3] the second one etc... 现在,$ url_array [0]通常定义您的控制器,$ url_array [1]定义您的操作,$ url_array [2]是第一个参数,$ url_array [3]是第二个参数,依此类推...

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

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