如何更改生成的错误网址

时间:2018-05-08 20:32:39

标签: php redirect

我将知道如何更改此元素。当我想更改网站上的语言时,问题就出现了

在我的索引(普通网址)中:

http://localhost/test/boutique/index.php?language=en
http://localhost/test/boutique/index.php?language=fr

在网站内导航不成问题。这是正确的

但如果进入我的产品 普通网址必须是:

localhost/test/boutique/index.php?Products&Description&products_id=1

如果我改变语言,我就有这个:

本地主机/测试/精品/ index.php的产品=安培;描述=安培; products_id = 1&安培;语言= FR

正确的网址必须是:

localhost/test/boutique/index.php?Products&Description&products_id=1&language=en

我认为有一个问题:=&

有相关的功能。

    public function getLanguageText() {
      $languages_string = '';

      $get_params = [];

      foreach ( $_GET as $key => $value ) {
// I think somethinf must change here.
        if (($key != 'language') && ($key != Registry::get('Session')->getName()) && ($key != 'x') && ($key != 'y')) {
          $get_params[] = $key . '=' . $value;
        }
      }

//var_dump($get_params)

      $get_params = implode($get_params, '&');

      if ( !empty($get_params) ) {
        $get_params .= '&';
      }

      foreach ($this->getAll() as $value) {
        $languages_string .= ' ' . HTML::link(OSCOM::link('index.php', $get_params . 'language=' . $value['code']), $value['name']) . ' ';
      }

      return $languages_string;
    }

var_dump($ get_params)是:

  0 => string 'Products=' (length=9)
  1 => string 'Description=' (length=12)
  2 => string 'products_id=1languageen' (length=23)  ===> pb here

如何纠正这个功能?

Tk的

关于:

的元素
var_dump($_GET);
var_dump($key);
var_dump($value);

array (size=4)
  'Products' => string '' (length=0)
  'Description' => string '' (length=0)
  'products_id' => string '2' (length=1)
  'language' => string 'fr' (length=2)

/home/www/test/boutique/includes/OM/Language.php:562:string 'Products' (length=8)

/home/www/test/boutique/includes/OM/Language.php:563:string '' (length=0)

/home/www/test/boutique/includes/OM/Language.php:561:
array (size=4)
  'Products' => string '' (length=0)
  'Description' => string '' (length=0)
  'products_id' => string '2' (length=1)
  'language' => string 'fr' (length=2)

/home/www/test/boutique/includes/OM/Language.php:562:string 'Description' (length=11)

/home/www/test/boutique/includes/OM/Language.php:563:string '' (length=0)

/home/www/test/boutique/includes/OM/Language.php:561:
array (size=4)
  'Products' => string '' (length=0)
  'Description' => string '' (length=0)
  'products_id' => string '2' (length=1)
  'language' => string 'fr' (length=2)

/home/www/test/boutique/includes/OM/Language.php:562:string 'products_id' (length=11)

/home/www/test/boutique/includes/OM/Language.php:563:string '2' (length=1)

/home/www/test/boutique/includes/OM/Language.php:561:
array (size=4)
  'Products' => string '' (length=0)
  'Description' => string '' (length=0)
  'products_id' => string '2' (length=1)
  'language' => string 'fr' (length=2)

/home/www/test/boutique/includes/OM/Language.php:562:string 'language' (length=8)

/home/www/test/boutique/includes/OM/Language.php:563:string 'fr' (length=2)

获取信息:

public static function link($ page,$ parameters = null,$ add_session_id = true,$ search_engine_safe = true){

  $page = HTML::sanitize($page);

  $site = $req_site = static::$site;

  if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && OSCOM::siteExists($matches[1], false)) {
      $req_site = $matches[1];
      $page = $matches[2];
  }

  if (!is_bool($add_session_id)) {
    $add_session_id = true;
  }

  if (!is_bool($search_engine_safe)) {
    $search_engine_safe = true;
  }

  if (($add_session_id === true) && ($site !== $req_site)) {
    $add_session_id = false;
  }

  $link = static::getConfig('http_server', $req_site) . static::getConfig('http_path', $req_site) . $page;

  if (!empty($parameters)) {
    $p = HTML::sanitize($parameters);

    $p = str_replace([
                      "\\", // apps
                      '{', // product attributes
                      '}' // product attributes
                      ], [
                        '%5C',
                        '%7B',
                        '%7D'
                      ], $p);



    $link .= '?' . $p;
    $separator = '&';
  } else {
    $separator = '?';
  }

  while((substr($link, -1) == '&') || (substr($link, -1) == '?')) {
    $link = substr($link, 0, -1);
  }

//从不同的HTTP和HTTPS服务器移动时,或者在定义SID时添加会话ID       if(($ add_session_id === true)&& Registry :: exists(' Session')){         $ OSCOM_Session = Registry :: get(' Session');

    if ($OSCOM_Session->hasStarted() && ($OSCOM_Session->isForceCookies() === false)) {
      if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
        $link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
      }
    }
  }

  while(strpos($link, '&&') !== false) {
    $link = str_replace('&&', '&', $link);
  }


  if ($search_engine_safe === true && defined('SEARCH_ENGINE_FRIENDLY_URLS') && (SEARCH_ENGINE_FRIENDLY_URLS == 'true') && SEFU::start() && static::getSite() != 'Admin') {
    $link = str_replace(['?', '&', '='], '/', $link);
  }

  return $link;
}

和SEFU:

类SEFU实现\ OM \ ServiceInterface {     public static function start(){

  if ( isset($_SERVER['ORIG_PATH_INFO']) ) {
    if ( isset($_SERVER['PATH_INFO']) && empty($_SERVER['PATH_INFO']) ) {
      $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
    }
  }

  if ( isset($_SERVER['PATH_INFO']) && (strlen($_SERVER['PATH_INFO']) > 1) ) {
    $parameters = explode('/', substr($_SERVER['PATH_INFO'], 1));

    $_GET = [];
    $GET_array = [];

    $i = 0;

    foreach ( $parameters as $parameter ) {
      $param_array = explode(',', $parameter, 2);

      if ( !isset($param_array[1]) ) {
        $param_array[1] = '';
      }

      if ( strpos($param_array[0], '[]') !== false ) {
        $GET_array[substr($param_array[0], 0, -2)][] = $param_array[1];
      } else {
        $_GET[$param_array[0]] = $param_array[1];
      }

      $i++;
    }

    if ( count($GET_array) > 0 ) {
      foreach ( $GET_array as $key => $value ) {
        $_GET[$key] = $value;
      }
    }
  }

  return true;
}

public static function stop() {
  return true;
}

1 个答案:

答案 0 :(得分:0)

在我看来,查询字符串被解析得很好。

<?php
parse_str('Products=&Description=&products_id=1&language=fr', $foo);

var_dump($foo);

$get_params = [];
foreach ($foo as $k => $v) {
  if (($k != 'language') && ($k != 'bar') && ($k != 'x') && ($k != 'y')) {
    $get_params[] = $k . '=' . $v;
  }
}

var_dump($get_params);

?>
array(4) {
  ["Products"]=>
  string(0) ""
  ["Description"]=>
  string(0) ""
  ["products_id"]=>
  string(1) "1"
  ["language"]=>
  string(2) "fr"
}

array(3) {
  [0]=>
  string(9) "Products="
  [1]=>
  string(12) "Description="
  [2]=>
  string(13) "products_id=1"
}

也许没有告诉你问题是什么,但我想我告诉你什么不是问题。

请分享您$_GET的转储。假设您正确复制了查询字符串...请参阅我的代码。我不知道mod_rewrite或其他可能发生的事情。没有看到$ _GET的转储,一旦它到达你的脚本,我就无法提供更多的见解。

<强>更新

这有帮助吗?老实说,不确定这一切是什么。可能甚至不值得一个功能...

<?php

function getLanguageText($q) {
  if(!isset($q['language'])) return false;
  return $q['language'];
}

//$q = $_GET;
$q = [
    'Products' => '',
    'Description' => '',
    'products_id' => 123,
    'language' => 'en',
];

echo getLanguageText($q); 
//> en