Trying to do a redirect

Can someone please tell me why this does not work. I have no doubt it is something really simple.

Your help will be VERY much appreciated

In the Body

<?php $qStr = "http://www.myexampapers.com/admin/form.php?tsk=".$_GET["tsk"]."&id=".$_GET["id"]."; Redirect ($qStr); //I have even substituted $qStr with "http://www.ibm.com in this line, without joy ?>

In the Header

Hi there,

There are two problems in your code.

Problem #1:
[php]$qStr = “http://www.myexampapers.com/admin/form.php?tsk=".$_GET[“tsk”]."&id=".$_GET[“id”].";[/php]
This line should not have the .” at the end. It should be ."" or removed completely as it is currently including the ; in the string at the end. (change the end to ."&id=".$_GET[“id”]:wink:

Problem #2:
You have set a function in javascript, and are calling it in PHP. If you need to call the function in PHP you need to create the function in PHP. An example one is below that works whether headers have already been sent or not.

[php]/**

  • Redirects to the page specified in $location
  • @param string $location Where to redirect to
  • @param integer $delay Delay in seconds
    */
    function redirect($location,$delay=0)
    {
    if(headers_sent() === true && $delay <= 0)
    {
    header(“Location: “.$location);
    }
    else
    {
    echo $delay == 0 ? ‘’
    : ‘’;
    echo ‘’;
    }
    }[/php]

Firstly, thanks so much “Smokey PHP” for taking the time to give me such a detailed response. Much appreciated.

I have implemented the change by trying the function in the section and in the body of my PHP file in a PHP wrapper (made small changes in reduceing the number of equals signs in two places, which I think were typos).

<?php function redirect($location,$delay=0) { if(headers_sent() == true && $delay <= 0) { header("Location: ".$location); echo 'alert('.$location.')'; } else { echo $delay = 0 ? '' : ''; echo ''; } } ?>

I have added the echo ‘alert(’.$location.’)’; statement to make sure I am getting to where I need to and it is working. I am passing the location http://www.ibm.com.

The outcome is that the file is displaying:
alert(http://www.ibm.com/)
but not redirecting to http://www.ibm.com/

Sorry for maybe being so stupid, but I am stuck.

Firstly, just so you are aware - those were not typos. === means ‘identical to’ (taking variable types into account) whereas just == means ‘equal to’ (just value, not variable type).

Changing the code to say this:

$delay = 0

Is currently setting $delay to 0 rather than checking if it is 0, you need to put that back to being ==

Correcting the ‘$delay = 0’ code should fix it. The alert() would not have worked by the way because it is not echoing it into script tags, change the line to:

echo '<script type="text/javascript">alert('.$location.');</script>';

I am forever in debt to you “Smokey PHP”. Thanks for being so patient. All works. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service