Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么rawurlencode()会添加额外的正斜杠?_Php_Urlencode_Html Entities - Fatal编程技术网

Php 为什么rawurlencode()会添加额外的正斜杠?

Php 为什么rawurlencode()会添加额外的正斜杠?,php,urlencode,html-entities,Php,Urlencode,Html Entities,有人能告诉我为什么rawurlencode()会把事情搞砸吗?添加一个额外的正斜杠。这是我的问题。 我正在构建分页的底部部分。谷歌类型分页 我的网址是: https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1 “搜索”是搜索词(要搜索的关键字)。 “tbl”是要搜索的mysql表。 “col”是要搜索的mysql

有人能告诉我为什么rawurlencode()会把事情搞砸吗?添加一个额外的正斜杠。这是我的问题。 我正在构建分页的底部部分。谷歌类型分页

我的网址是:

https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1
“搜索”是搜索词(要搜索的关键字)。 “tbl”是要搜索的mysql表。 “col”是要搜索的mysql表列

我的编码尝试:

<?php
//Url: https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1
$selfpage = $_SERVER['PHP_SELF']; //https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1
$search = 'keyword';
$tbl = 'links';
$col = 'keyword';
$max = '100'; //Quoted.
$page = '1'; //Quoted.

$path = rawurlencode($selfpage); //FAILS.
//$path = $selfpage; //WORKS.
$query_string = '?search=' .urlencode($search) .'&tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&max=' .intval($max) .'&page=' .intval($page);
$url = $path .htmlentities($query_string);
echo '<a href=' .'"' .$url .'"' .'>Next</a>'; echo '<br>';
?>
如果我忘记了rawurlencode(),则会输出正确的url: https://localhost/Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1

但是如果我像这样包含rawurlencode():

然后链接变成这样: https://localhost/Templates//Templates/url_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1

注意“/”为什么它变成双正斜杠而不是单正斜杠? 为什么它会重复文件夹“模板”? 无论如何,当我单击此链接时,我的浏览器会将我转发到错误或无效的url: https://localhost/Templates/%2FTemplates%2Furl_encode_Template.php?search=keyword&tbl=links&col=keyword&max=100&page=1

我不知道在哪里添加rawurlencode()和htmlentities(),所以有人能告诉我在哪里添加rawurlencode()和在哪里添加htmlentities()?似乎我在正确的位置添加了urlencode()。对吗

在教程中,我了解到您使用以下格式进行操作: urlencode($\u获取值); rawurlencode(文件前的文件路径?); htmlentities(查询字符串-在查询之前和之后?)


谢谢

“rawurlencode(在?)之前的文件路径”-但是您没有在这里对URL的路径部分进行编码,而是对整个URL进行编码。(一次编码完整路径也会出错,然后所有
/
也会被编码,这意味着它们不再充当“目录分隔符”。每个路径段(斜杠之间的部分)都需要单独编码,然后用一个字面值/在它们之间重新连接在一起。)@CBroe,我明白你的意思。尽管如此,我仍然被困在如何继续下去的问题上。你介意给我看一点吗?这是我的url示例:@CBroe,如果我可以破坏$_SERVER['PHP_SELF']获取的url,我可能可以开始。我不记得php函数将url分成几个部分(路径(服务器、目录)、查询等)。
php\u SELF
应该只包含url的路径部分,所以您上面所说的(它包含完整的绝对url)一开始可能不是真的。为什么还要为这条路烦恼呢?如果您想保持在相同的URL路径中,那么只需使用相对URL,它只包含查询字符串部分。不要通过手动连接参数名和URL编码值来组装查询字符串-
http\u build\u query
exists.@CBroe,我使用的是$\u SERVER['PHP\u SELF'],所以PHP会获取文件名(rel URL)。我现在意识到情况并非如此,它获取的文件路径不包括服务器名。所以现在,我不得不放弃$_服务器['PHP_SELF']。我可以用什么来替换它以仅获取文件名?
$path = rawurlencode($selfpage); //FAILS.
//$path = $selfpage; //WORKS.
$path = rawurlencode($selfpage); //FAILS.