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.
Link to comment
Share on other sites

[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...
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

$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?
Link to comment
Share on other sites

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".
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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().
Link to comment
Share on other sites

<?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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.