URL中用到的一些编码方式

本文介绍了如何使用urlencode、base64_encode及htmlentities等方法进行数据编码,特别关注在页面间传递数据时的应用。通过实例展示了这些函数的具体用法,并解释了它们在实际项目中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到项目代码中有这样一些方法:urlencode, base64_encode, htmlentities等。

网上找了一些资料如下:
string urlencode ( string $str )
      This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
      在页面间传递数据时可以使用到该方法。
它的返回值:
      Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent ( %) sign followed by two hex digits and spaces encoded as plus ( +) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.
      非文字、且不是 -_.的,会被编码为百分号(%)打头的两位16进制数,空格被编码为加号。比如:"browse.myspace.cn/;:"被解析为"browse.myspace.cn%2F%3B%3A"。其中的/;:都被编码了。

string htmlentities ( string $string [, int $quote_style [, string $charset [, bool $double_encode ]]] )     
      This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
      If you're wanting to decode instead (the reverse) you can use html_entity_decode().

See Also


string base64_encode ( string $data )
     

Encodes the given data with base64.

This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

Base64-encoded data takes about 33% more space than the original data.

一种编码方式,需要占用更多的空间。



在我们的项目中,为了获取一个url,用来供未登陆者,先登陆再返回当前页面使用,其中return_url指向当前页面。

$url = $ms_login_url_prefix;
    $url .= "?t=".urlencode(base64_encode($return_url));
    $k = md5($return_url.$ms_login_secret_key);
    $url .= "&k=".urlencode($k);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值