简体   繁体   English

CodeIgniter不允许在查询搜索词中使用等号?

[英]CodeIgniter not allowing equal sign in query search term?

I have a controller that is accepting a encoded code and setting it as a session variable. 我有一个控制器,它接受编码的代码并将其设置为会话变量。 I am then trying to look up that code in the DB. 然后,我试图在数据库中查找该代码。

When I print out the query string, it is being excluded for some reason. 当我打印出查询字符串时,由于某种原因它被排除在外。

/**
 * Pass our signup code for demo purposes
 */
public function c(){

    // Did we come here from a Sign-up Pin?
    $registerPin = $this->uri->segment(3);

    if($registerPin){
        $this->session->set_userdata(array(
            'registerPin'  => $registerPin
        ));
    }

    // Redirect to register
    redirect(site_url("register"));

}

Model: 模型:

/**
 * Check to see if a pin is a valid demo pin.
 */
public function check_register_pin($pin){

    $s=$this->db
        ->where(array("p.IsDemoPin" => '1', "p.Denomination" => 20, "p.Pin" => $pin))
        ->where("PinID NOT IN (select PinID from customer_pins where `PinID` = '$pin')")
        ->get("pins as p");

    // If this pin was valid and not used, return true.
    if ($s->num_rows() > 0) {
        return true;
    } else {
        return false;
    }

}

Config: 配置:

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_=+-';

Example Pin I am trying to look up: 我试图查找的示例Pin:

ebe83beb61277ab20882b68444cc93e10391d230758847f85e97428d95b7022aab6cdabff6cac097f2e79ceb2df56e06b227063f627341b97346abdc03106d28sXhvJ2tlUzkAGdGwjuKM6O137GlP5tQ1kvazNFnj/f4=

Printed Query: 打印查询:

SELECT * FROM `pins` as `p` WHERE `p`.`IsDemoPin` = '1' AND `p`.`Denomination` = 20 AND `p`.`Pin` = 'ebe83beb61277ab20882b68444cc93e10391d230758847f85e97428d95b7022aab6cdabff6cac097f2e79ceb2df56e06b227063f627341b97346abdc03106d28sXhvJ2tlUzkAGdGwjuKM6O137GlP5tQ1kvazNFnj' AND `PinID` NOT IN (select PinID from customer_pins where `PinID` = 'ebe83beb61277ab20882b68444cc93e10391d230758847f85e97428d95b7022aab6cdabff6cac097f2e79ceb2df56e06b227063f627341b97346abdc03106d28sXhvJ2tlUzkAGdGwjuKM6O137GlP5tQ1kvazNFnj')

The problem is, the class your using to encrypt the value returns a base64 encoded string. 问题是,您用来加密值的类返回一个base64编码的字符串。 Which contains a / and it breaks your routing when used in the url. 其中包含一个/并且在url中使用时会中断您的路由。

Base64 is not url safe because it contains / , + and = characters. Base64 不是网址安全的,因为它包含/+=字符。

You could work around this issue by decoding what the function returns and then encoding it in a url safe way, which you can then use without issues in the url path: 您可以通过解码函数返回的内容然后以url安全的方式对其进行编码来解决此问题,然后可以在url路径中使用它而不会出现问题:

<?php
/**
 * Decode a url-safe base64 string.
 */
function base64_urldecode($str) {
    $pad = strlen($str) % 4;
    if ($pad) {
        $padlen = 4 - $pad;
        $str .= str_repeat('=', $padlen);
    }
    return base64_decode(strtr($str, '-_', '+/'));
}

/**
 * Encode a string into url safe base64.
 */
function base64_urlencode($str) {
    return str_replace('=', '', strtr(base64_encode($str), '+/', '-_'));
}

$code = base64_decode('ebe83beb61277ab20882b68444cc93e10391d230758847f85e97428d95b7022aab6cdabff6cac097f2e79ceb2df56e06b227063f627341b97346abdc03106d28sXhvJ2tlUzkAGdGwjuKM6O137GlP5tQ1kvazNFnj/f4=');

echo base64_urlencode($code);

Would output _f4 instead of /f4= : 将输出_f4而不是/f4=

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

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