# Solved: PHP: Can't get header('location...') to work.



## twmprys (Jun 4, 2009)

I've got a page which lists the records in a table. When you press "delete" next to a record, that record is deleted. I want the page to reload as you delete so that the deleted record is not shown. I though this was the function of header 'location'. I can't make it work, however. Is there a better way of doing this?


```
if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM table WHERE id={$_GET['id']}";
    $result = mysql_query($sql);
	header('location: thispage.php');

	 }
```


----------



## nj2b (Sep 9, 2006)

If you've outputted anything to client prior to calling header, then it won't work. header must be called before any output is sent to the client. That includes any scripts that you "require" There cannot be any white spaces after the ending ?>

You could try using ob_start() at the beginning of your script and then call ob_end_flush() at the end.

You can rely on javascript as a last resort


```
<?php
echo "&lt;script type=\"text/javascript\"&gt;window.location = 'index.php';&lt;/script&gt;";
?>
```
Remove the < > pairs... this code thing wants to execute the script for whatever reason. 

HTH


----------



## twmprys (Jun 4, 2009)

The ob_ commands just give me a blank screen.

I can't understand why the header instruction doesn't work - it works on the same page for a form submit:


```
$submit = $_POST['form']; 
if (isset($submit)) {
 $query ="INSERT INTO table (name)";
    $query.="VALUES ('{$_POST['name']}')";
    $result=mysql_query($query);

	header("location: thispage.php");
}
```


----------



## nj2b (Sep 9, 2006)

Have you checked to verify that the if condition is being entered? Add an echo "all ok" or something.

I've never had much luck using a comparison test "==" on $_GET or $_POST but the identity test "===" seems to work well. On a side note, you might want to validate the $_GET or $_POST var before using it directly in your sql. As it is now, someone could use sql injection.


----------



## twmprys (Jun 4, 2009)

Thanks for your advice.

I'm not too concerned at the moment about injection because the page is password protected for an individual user.

I've tried echoing something after the 'if' statement and it works fine. I just can't understand why it's not reloading the page - I have to manually reload the page to get the deletion to register.


----------



## nj2b (Sep 9, 2006)

Have you checked to see if your query is successful? $result = mysql_query($query) or die(mysql_error()); Another thing you might try is viewing the page source after you send the get request. If something is failing, you may get any error(s) that way. If it's making it to the header function, the script *may* be sending out garbage that prevents header() it from working. When you echoed something, did you get the can't send headers error?


----------



## twmprys (Jun 4, 2009)

When I put the header line back in, I get an error 'Cannot modify header information' and it refers me back to a line that doesn't seem to have any header information in it.


----------



## nj2b (Sep 9, 2006)

If you haven't been doing so already, try this. I must admit that this is a bizarre problem.


```
<?php

[COLOR=#000000][COLOR=#007700]ob_start();
... // any other code

[/COLOR][/COLOR] [COLOR=#000000][COLOR=#007700]if([/COLOR][COLOR=#0000bb]$_GET[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]"cmd"[/COLOR][COLOR=#007700]] ==[/COLOR][COLOR=#dd0000]= "delete"[/COLOR][COLOR=#007700]) { 
    [/COLOR][COLOR=#0000bb]$sql [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"DELETE FROM table WHERE id={$_GET['id']}"[/COLOR][COLOR=#007700]; 
    [/COLOR][COLOR=#0000bb]$result [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]mysql_query[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$sql[/COLOR][COLOR=#007700]) or die(mysql_error());
 
    [/COLOR][COLOR=#0000bb]header[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]'location: thispage.php'[/COLOR][COLOR=#007700]);    
}[/COLOR][/COLOR]

... // any other code until the closing ?>
[COLOR=#000000][COLOR=#007700]ob_flush();
?>
[/COLOR][/COLOR]
```


----------



## twmprys (Jun 4, 2009)

Wow, that works! You're a star, thanks very much!


----------



## MMJ (Oct 15, 2006)

What editor are you using?

It could be the UTF-8 Byte Order Mark (BOM).


----------



## twmprys (Jun 4, 2009)

Dreamweaver.


----------

