drsdvwsvo78320812 2009-12-20 21:03
浏览 32
已采纳

URL视图助手中URL编码参数的问题

So my problem is kinda weird, it only occurs when I test the application offline (on my PC with WampServer), the same code works 100% correctly online.

Here is how I use the helper (just example):

<a href="<?php

echo $this->url(array('module' => 'admin',
                      'controller' => 'index',
                      'action' => 'approve-photo',
                      'id' => $this->escape($a->id),
                      'ret' => urlencode('admin/index/photos')),
                null,
                true);

                            ?>" class="blue">approve</a>

Online, this link works great, it goes to the action which looks similar to this:

public function approvePhotoAction()
{
    $request = $this->getRequest();
    $photos = $this->_getTable('Photos');

    $dbTrans = false;

    try {

        $db = $this->_getDb();
        $dbTrans = $db->beginTransaction();

        $photos->edit($request->getParam('id'),
                      array('status' => 'approved'));

        $db->commit();

    } catch (Exception $e) {
        if (true === $dbTrans) {
            $db->rollBack();
        }
    }

    $this->_redirect(urldecode($request->getParam('ret')));
}

So online, it approves the photo and redirects back to the URL that is encoded as "ret" param in the URL ("admin/index/photos").

But offline with WampServer I click on the link and get 404 error like this:

Not Found

The requested URL /public/admin/index/approve-photo/id/1/ret/admin/index/photos was not found on this server.

What the hell?

I'm using the latest version of WampServer (WampServer 2.0i [11/07/09]). Everything else works.

Here is my .htaccess file, just in case:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

# Turn off magic quotes
#php_flag magic_quotes_gpc off

I'm using virtual hosts to test ZF projects on my local PC. Here is how I add virtual hosts.

httpd.conf:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName myproject
    DocumentRoot "C:\wamp\www\myproject"
</VirtualHost>

the hosts file:

127.0.0.1    myproject

Any help would be appreciated because this makes testing and debugging projects on my localhost a nightmare and almost impossible task. I have to upload everything online to check if it works :(

UPDATE:

Source code of the _redirect helper (built in ZF helper):

/**
 * Set redirect in response object
 *
 * @return void
 */
protected function _redirect($url)
{
    if ($this->getUseAbsoluteUri() && !preg_match('#^(https?|ftp)://#', $url)) {
        $host  = (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'');
        $proto = (isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=="off") ? 'https' : 'http';
        $port  = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
        $uri   = $proto . '://' . $host;
        if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
            $uri .= ':' . $port;
        }
        $url = $uri . '/' . ltrim($url, '/');
    }
    $this->_redirectUrl = $url;
    $this->getResponse()->setRedirect($url, $this->getCode());
}

UPDATE 2:

Output of the helper offline:

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

And online (the same):

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

UPDATE 3:

OK. The problem was actually with the virtual host configuration. The document root was set to C:\wamp\www\myproject instead of C:\wamp\www\myproject\public.

And I was using this htaccess to redirect to the public folder:

RewriteEngine On

php_value upload_max_filesize 15M
php_value post_max_size 15M
php_value max_execution_time 200
php_value max_input_time 200
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]

Damn it I don't know why I forgot about this, I thought the virtual host was configured correctly to the public folder, I was 100% sure about that, I also double checked it and saw no problem (wtf? am I blind?).

  • 写回答

1条回答 默认 最新

  • dqs66973 2009-12-20 21:17
    关注
    1. check your php.ini
      php_flag magic_quotes_gpc off Must be Off at online and local servers

    2. do not use urlencode 'ret' => urlencode('admin/index/photos')) because url helper has fourth parameter $encode = true and it is true by default

    3. show us the output of $this->url(array('module' => 'admin'..... at server and at localhost

    i'm using wamp at home and it works like charm

    UPDATE 1
    try $this->_redirect( '/'. ltrim(urldecode($request->getParam('ret')), '/') );

    UPDATE 2;

    ViewScript

        <a href="<?php
    
    echo $this->url(array('module' => 'default',
                      'controller' => 'index',
                      'action' => 'test-encoded-redirect',
                      'ret' => urlencode('default/static/faq')),
                null,
                true);
    
                            ?>">test me</a>
    
    
        <?php
        class IndexController extends Smapp_Controller_Action 
        {   
    
            public function testEncodedRedirectAction()
            {
    
                                // ddump($this->_getAllParams());
                // ddump output 
                // Array ( 
                //  [controller] => index 
                //  [action] => test-encoded-redirect 
                //  [ret] => default%2Fstatic%2Ffaq 
                //  [module] => default 
                // )
    
    
                $ret = '/' . ltrim(urldecode($this->_getParam('ret')), '/');
                // echo $ret;
                // output: /default/static/faq
    
                $this->_redirect($ret);
            }
        }
    

    it works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!