PDA

View Full Version : Redirect with Sessions



dswimboy
04-07-2004, 12:10 AM
I am using sessions to authorize users. I would like to re-direct a user from one page to another, but using sessions sends header information immeadiately. include() doesn't work either. how can i re-direct users after sending session headers?

DesignersToolz
04-07-2004, 12:15 AM
Just print a meta refresh tag to the page :(

raf
04-07-2004, 12:17 AM
I would like to re-direct a user from one page to another, but using sessions sends header information immeadiately. :confused:
Are you sure?
I do that all the time. I have a session_start() on top of each page (inside an include of course) and i can redirect just fine. We are talking about stuff like


session_start();
$_SESSION['test']='going to redirect';
header('Location: test.php');
die();

right ?

dswimboy
04-07-2004, 12:55 AM
when using
session_start();
header("Location: list.php");i get the error
Warning: Cannot modify header information - headers already sent by (output started at /home/web/save.php:9) in /home/web/save.php on line 25

i'll use the meta-refresh thingy...

raf
04-07-2004, 01:00 AM
The code you posted is not the code you actually used to get the error.
Post the complete code --> all the 25 top lines.

dswimboy
04-07-2004, 01:33 AM
session_start();
if (!isset($_SESSION['authed']) || $_SESSION['authed'] != true) {
require 'functions.php';
display_login();
exit;
}
$file = $_POST['file'];
echo "<P>$file</P>";
$rte = $_POST['rte1'];
echo "<DIV>$rte</DIV>";
$handle = fopen('pages/'.$file, "r");
$contents = fread ($handle, filesize ('pages/'.$file));
fclose($handle);
if (!is_writable($file)) {
echo "<P>$file is not writeable</P>";
}
$array = explode('<!-- editable content -->', $contents);
$array[1] = $_POST['rte1'];
$handle1 = fopen('pages/'.$file, "w");
$string = implode("<!-- editable content -->", $array);
fwrite($handle1, $string);
fclose($handle1);

header("Location: list.php");

raf
04-07-2004, 01:36 AM
If you want to redirect, then NO OUTPUT should have been sent to the browser. The

echo "<P>$file</P>"; // Line 9 probably
echo "<DIV>$rte</DIV>";
echo "<P>$file is not writeable</P>";

shouldn'ty be there.
It's the first of these echo's that causes the error.

dswimboy
04-07-2004, 01:51 AM
thanks for fixing it.

i was being really dumb. i added those in before for some debugging, and never even crossed my mind.

raf
04-07-2004, 01:54 AM
You're welcome.

It's always little things like this (PS : trust the errormessages ;) )

jediman
04-07-2004, 03:04 PM
You can also use a function :)
Thats what I ended up doing :) And it works great :)



Function redirect()
{


header('Location: ./index.php');


}

basically :)
Just be sure there isnt any html tags in just yet, cause that was screwing my pages up there ;)

Nightfire
04-07-2004, 03:40 PM
Well that function is completely useless. You're just adding pointless extra filesize to do exactly the same as what the header would do on it's own