-[RESOLVED]- Header. Give me response.redirect anyday.. Can you turn me back?
Code:
<?php
switch ($_GET["action"]) {
case "auth":
echo "you are at auth";
break;
case "sum":
echo "you are at sum";
break;
case "phpinfo":
phpinfo();
break;
default:
header( 'location:http://localhost:1987/php_dev/index.php?action=auth' );
exit;
}
?>
Ok, heres the output.
Notice: Undefined index: action in C:\Program Files\IIS\Inetpub\wwwroot\Site\php_dev\index.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\IIS\Inetpub\wwwroot\Site\php_dev\index.php:2) in C:\Program Files\IIS\Inetpub\wwwroot\Site\php_dev\index.php on line 21
There is NOTHING outputting before the header so why is it complain.
As you can tell i'm trying to transfer from asp.net to php because the servers are cheaper.
So why is this header throwing a wobbly and what is the equal to response.redirect() of asp.net the one that you can redirect at ANY point in your script. PS. META tags are to messy.
Re: Header crap. Give me response.redirect anyday.. Can you turn me back?
I'm going to takle your questions in reverse.
First why the cannot send headers info: 1) Make sure there is NOT BLANK lines or anything at the top. Even a single whitespace is considered output. Secondly, the error generated by line resulted in output (the error msg). So Once you get that straightended out, it should go away.
Ok, now for the error on line 2. You really should check to see if the $_GET[] is set first:
Code:
if (isset($_GET["action"])) {
$Action = $_GET["action"];
}else{$Action='';}
Then use $Action in your switch:
Code:
switch ($Action) {
case "auth":
echo "you are at auth";
break;
case "sum":
echo "you are at sum";
break;
case "phpinfo":
phpinfo();
break;
default:
header( 'location:http://localhost:1987/php_dev/index.php?action=auth' );
exit;
}
?>
To attempt to access it w/o first making sure it's there is akin to acessing an object before instanciating it.
Tg
Re: Header crap. Give me response.redirect anyday.. Can you turn me back?
ok, you may of pulled me back to PHP.
One question is;
is there a redirect command that can me used at any point?
The Response.Redirect command is useable at anytime. Is there one for PHP
Re: Header crap. Give me response.redirect anyday.. Can you turn me back?
Quote:
Originally Posted by DigitalMyth
ok, you may of pulled me back to PHP.
One question is;
is there a redirect command that can me used at any point?
The Response.Redirect command is useable at anytime. Is there one for PHP
Yes and no. Firstly it is best to have a redirect at the top of your script before any output as it makes the code more readable to other developers.
Should the need still arise to send a header after output has been sent you can use output buffering. An output buffer is a place where all output from the script is held until the script is done executing. Once the script has finished the buffer is flushed and all the data is sent to the client in one big chunk.
To turn on the ouput buffer just add this as the top line of your script:
PHP Code:
<?php
ob_start();
?>
You can do some pretty neat things with output buffers, such as nesting and compression so it is also worth looking at the other functions which relate to ouput buffering.
http://uk.php.net/manual/en/ref.outcontrol.php
Re: Header crap. Give me response.redirect anyday.. Can you turn me back?
There WAS output before the redirect: the notice that was generated by accessing a non-existent array element.
Re: Header crap. Give me response.redirect anyday.. Can you turn me back?
Quote:
Originally Posted by CornedBee
There WAS output before the redirect: the notice that was generated by accessing a non-existent array element.
Isn't that what I said? :p
Tg
Re: Header. Give me response.redirect anyday.. Can you turn me back?
Yes, but the problem is so common that your mention was not prominent enough.