传递十六进制作为参数失败

时间:2015-02-07 22:00:01

标签: php

我试图像这样从vanilla php调用一个codeigniter方法。

get.php

<?php
$options = array(
    'vars' => array(
        'one'   => 'hello',
        'two'   => 'world'
    )
);
function strToHex($string){
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}


$p1 = $options["vars"]["one"];
$p2 = $options["vars"]["two"];

$en = serialize($options);
$tohex = strToHex($en);

try{
echo file_get_contents("http://localhost/app/welcome/entry/".$tohex);
$json = file_get_contents("http://localhost/app/welcome/entry/".$tohex,true);
if ($json === false) {
    echo 'Request with id'.' '.rand(5667789,790637738).' '.'was not successful.This incident was logged.';
}
}
catch(Exception $e){
echo $e->getMessage();
}

?>

这是我的codeigniter方法

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
    public function index()
    {
        $this->load->view('welcome_message');
    }
    public function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
    }
    public function entry($in){
    $in = $this->uri->segment(3);
    $arr = hexToStr($in);
    $array = unserialize($arr);
    print_r($array);
    /**
    $p1 = $arr["vars"]["one"];
    $p2 = $arr["vars"]["two"];
    redirect('welcome/'.$p1/$p2);
    */
    }

    public function hello(){
    echo 'connected';
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

当我运行get.php时,我得到一个空白屏幕。为什么会出现这种情况?。我有错误报告。原因可能是我作为参数传递的十六进制数。?

1 个答案:

答案 0 :(得分:0)

我使用了hex2bin函数,现在我的代码按预期工作了

public function entry($in){
    $in = $this->uri->segment(3);
    $arr = hex2bin($in);
    $array = unserialize($arr);
    $p1 = $arr["vars"]["one"];
    //print_r($array);
    $p1 = $array["vars"]["one"];
    echo $p1;
    /**
    $p1 = $arr["vars"]["one"];
    $p2 = $arr["vars"]["two"];
    redirect('welcome/'.$p1/$p2);
    */
    }