Jump to content

why does header location (redirection) not work?


ted_chou12

Recommended Posts

[code]
<?php
if ($message != "" && $title != "")
{$file = fopen("newscomments/comments[$number].txt","w+");
fwrite($file,"$title;seDp#$datetime;seDp#$message\r\n");
foreach ($txtfile as $txtfile) {fwrite( $file, "$txtfile");}
fclose($file);$title = "";$message = "";header("location: include.php?page=newsconfirm");}
?>
[/code]
this is how I used it, but after writing the page doesnt redirect to the confirm page at all, it simply stays on the writing page, (it does write to the text file.
Can anyone have a look at this?
Thanks Ted.
[code]<?php
if ($message != "" && $title != "") // do you need to do isset() on $message and $title as well?
  {$file = fopen("newscomments/comments[$number].txt","w+"); //where is $number declared?
    fwrite($file,"$title;seDp#$datetime;seDp#$message\r\n");  //where is $datetime declared?
    foreach ($txtfile as $txtfile) // What I don't think this is needed...?
              {fwrite( $file, "$txtfile");} // ditto
    fclose($file);
    $title = "";
    $message = "";
    header("location: ./include.php?page=newsconfirm");}
?>[/code]

I can't figure what you are trying to do here...
this is the first part to it:
[code]
<?php
if(isset($_POST['submit'])){
$number = $data[$news][2];
$txtfile = file("newscomments/comments[$number].txt");
$title = $_POST['title'];
$message = $_POST['message'];
$datetime = date("M d, Y");
?>
I didnt put it in previously because I though it isnt that important...[/code]
$username = $_SESSION['sconlineusername'];
require("../mysqlconnection.php");
$sql = "SELECT * FROM usercus WHERE username = '$username'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0){header("location: editprofile.php");}

this one doesnt work as well any other command works but head location doesnt
like i tried echo, print they all work, but not redirection...
why is that?
is there a bug?
header requires a full path be sent. Or at least it's supposed to. So:

[code]if (isset($_SERVER["HTTP_HOST"])) $host = $_SERVER["HTTP_HOST"];
else $host = $_SERVER["SERVER_NAME"];
$uri = dirname($_SERVER["PHP_SELF"]);
if ($uri === "\\") $uri = "/";
if ($uri[strlen($uri) - 1] !== "/") $uri .= "/";
header("Location: http://" . $host . $uri . "include.php?page=newsconfirm");[/code]

Also, AFAIK the header function is case-insensitive in regards to the HTTP header choice (i.e., "Location" === "location"). But I could be wrong about this, so it's probably safest just to use "Location".
nope that doesnt work either, okay, i read the sticky, but i still dont quite understand what they are talking about, actually, i do have login session and cookies,
the page looks something like this:
[code]
<?php include("login.php");
require("../mysqlconnection.php");
$sql = "SELECT * FROM usercus WHERE username = '$username'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0){header("Location: http://sconline.66ghz.com/users/editprofile.php");}?>
[/code]
before and after are just html codes, not related
and login.php contains cookies and sessions...
the above header location is the one which I am talking about...
yeap thorpe, so i prefer all lower cases, easier to read
Thanks
TEd
[quote]before and after are just html codes, not related[/quote]

Read the thread again. You cannot have ANY output sent to the browser (including any html) before calling the header() function. Turn error reporting on and you will see a 'headers allready sent error'.

Re organize your code so that there is no html outputted before your call to header().
<?php
$command  = header("location: editprofile.php");
?>
html code
<?php include("login.php");
require("../mysqlconnection.php");
$sql = "SELECT * FROM usercus WHERE username = '$username'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0){$command;}?>
should this work them?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.