# Php Header



## Moose (Sep 17, 2008)

Can anyone see anything wrong with this code?
if ($num_rows > 0) {
 session_start();
 $_SESSION['login'] = "1";
 header ("Location: index.php");
}
It's part of my simple login script with Mysql it does the session ID bit ok so I am logged in but I don't get directed to index.php instead I stay on the page? This script used to work but it suddenly stopped on the two pages I was using it on, and after submitting it stayed on the page.


----------



## Disparia (Sep 18, 2008)

I believe you need a complete URL for the location header.


```
header("Location: http://www.yoursite.com/index.php");
```


----------



## Moose (Sep 18, 2008)

it still doesn't work, it doesnt even try just reloads the login page.


----------



## ZenZimZaliben (Sep 18, 2008)

Are you running frames? If so you need to add target="_parent"

You could also try a include function instead of a redirect.
include("../dir/page.php");


----------



## Disparia (Sep 19, 2008)

Are we sure that $num_rows is greater than 0 at that point?

Or maybe headers/output has already been sent before header() is called? If you're on shared hosting, display_errors is usually off and only a warning is thrown so the script will continue to run. Even whitespace characters are considered output.


----------



## W1zzard (Sep 19, 2008)

you need to exit(); after sending the redirect header. header just sends the header and continues with execution


----------



## Moose (Sep 19, 2008)

Im hosting it in my home,


Jizzler said:


> Or maybe headers/output has already been sent before header() is called? If you're on shared hosting, display_errors is usually off and only a warning is thrown so the script will continue to run. Even whitespace characters are considered output.


I think you may have it there I just fixed the error thing and it came up with a load of PHP crap about the header already being sent, so I just wacked the login script (with the header part) in a new page, seperate from all the other PHP and HTML on the page further up, then linked the login form to it, so it must of ben something else.


----------



## W1zzard (Sep 20, 2008)

see my reply above.


----------



## Moose (Sep 27, 2008)

Thanks, it works with both strangely Wizzard, I would now like to make it so instead of viewing index.php it goes to the page before last viewed (the previous page being the login one, so isn't wanted)
Btw if anybody wants to look at the site its located here and made from scratch by me fairoaks.qupis.com though doesn't work with IE for some strange reason.

```
if ($num_rows > 0) {
session_start();
$_SESSION['login'] = "1";
header ("Location: What do I put here to take back 2 pages?");
exit();
}
```


----------



## W1zzard (Sep 27, 2008)

after location put the address whereever you want it to go.


----------



## Moose (Sep 27, 2008)

The trouble is I dont know where they have come from, lots of pages that cannot be viewed by unregistered users, and they all redirect to the login page, i would like the login page to set the header to whatever page they have come from, rather than the set index.php page.


----------



## W1zzard (Sep 27, 2008)

options:
1) pass the "current" web page url to the login form in a hidden form field
2) use javascript code to go back 2 pages (won't work on js disabled browsers)
3) redirect the users to one page and live with it


----------



## Moose (Sep 27, 2008)

I think i'=ll live with it, I still need to make it IE compatible which is more important


----------

