Fork me on GitHub

pnRedirect  Bottom

Go to page 1 - 2 [+1]:

  • Hey guys

    I have no idea why but pnRedirect does absolutely nothing for me:
    pnRedirect('http://www.postnuke.com');

    Nothing happens - good or bad :) (although I guess it's bad that nothing happesn)

    Another module at my site is using it without problems!

    Anyone know what might be wrong?
  • pnRedirect uses the PHP header() function to perform a redirect. This will not work if you output anything to the browser before a pnRedirect call, i.e you can't use echo or anything else outputting content.

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • I tried with this as the entire main function:

    function Test_admin_main() {
    pnRedirect('http://www.postnuke.com/');
    return "Was not redirected";
    }

    And "Was not redirected" is shown :\
  • Try

    Code

    function Test_admin_main() {
    pnRedirect('http://www.postnuke.com/');
    return true;
    }


    HTH
  • Nope, doesn't work.

    Shouldn't matter what you write after the pnRedirect in any event since you should be redirected, right?
  • Sorry, you're right. The term brain-fart comes to mind icon_redface

    How about

    Code

    function Test_admin_main() {
       return pnRedirect('http://www.postnuke.com/');
    }
  • I've tried it - doesn't work.

    In the Example module they do this to navigate between functions though, which is what I want to use it for, but I don't get it working for that either.

    The code you wrote there outputs the "failed to load module...." message, because it doesn't return any output.

    I'm starting to get angry at this thing now! :)
  • What happens if you paste

    Code

    pnRedirect('http://www.postnuke.com/');


    in your index.php just below the lines with

    Code

    // start PN
    pnInit();
  • The important piece of info was provided by Simon. pnRedirect will not work (at all) if there has already been some output (even whitespace) by the script - actually it's if the headers have been sent back to the browser.

    A simple check for this is using the headers_sent function.

    Code

    if (headers_sent()) {
        echo 'cannot readirect as headers have already been sent';
    } else {
        return pnRedirect('http:://......');
    }


    The problem then we be indentifying where you stray output has come from; possibly an earlier debug line, whitespace before/after PHP tags etc.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Thanks for that helpful code Mark!

    I have found the source to my problem. I require_once a file which holds my database connection (not only using the PN database for this module). So at the top of pnadmin.php I

    Code

    require_once('dbconfig.php');


    I tried having only one line of code in dbconfig.php:

    Code

    $dbconnection = mysql_connect('xxx', 'xxx', 'xxx');
    I still have the same problem: headers are sent. I then move this line into the pnadmin.php file and replace the require_once statement, and now it works.

    Okay, so I guess require_once is sending something, although I had no idea it did and I still don't know why it does. In any event, I guess I shouldn't be using require_once here? What's the correct way to go at it when using another database in addition to the PN database?

    I really appreciate your help guys, and that goes to those who had suggestions which didn't lead to a solution also - thanks for trying! :)
  • You say that file only has the one line in it? Try putting PHP delimiters into the file i.e.

    Code

    <?php your code ?>
    .

    -Mark



    edited by: pheski, Nov 29, 2006 - 11:32 PM

    --
    Visit My homepage and Zikula themes.
  • Quote

    Shouldn't matter what you write after the pnRedirect in any event since you should be redirected, right?


    It shouldn't but it does and different browsers handle this differently (some work and some don't). Best to make sure you return true to PostNuke.
  • Mark: I already had PHP delimiters there

    ctimmer: they do the same for me, none of them work (firefox, IE (new beta) and opera). In any event, postnuke doesn't want true though does it? PN wants some string of output, and true will cause it to fail loading(?)

    The redirect doesn't work if I require_once the DB file with this content:

    Code

    <?php $dbconnection = mysql_connect('xxx', 'xxx', 'xxx'); ?>

    and if I copy paste it over to pnadmin.php and remove the require_once statement then it works icon_rolleyes

    Very little code needed to test this.. could someone have a go if this is not suppose to happen?
  • The redirect tells the browser to use a different URL.

    The 'return(true)' tells PostNuke that your app has handled the browser output (in this case the redirect) and to not output anything else.

    When PHP sees any kind of output, it automatically generates the required headers that will nullify your redirect. You may see an indication of this in your error log files.
  • If your MySQL connect statement is returning an error or notice, this counts as output and pnRedirect will no longer work. So, are you sure that mysql_connect() is not producing an error?

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide

Go to page 1 - 2 [+1]:

This list is based on users active over the last 60 minutes.