开发者

Problem with URL encoded parameters in URL view helper

开发者 https://www.devze.com 2022-12-14 17:00 出处:网络
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.

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. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消