header() question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

header() question

Post by speedy33417 »

I'm getting stuck with a header issue. I have a login page at mysite.com/login.php and I'd like to direct any unathorized access to this page from any part of the site.
My problem is if the user tries to see mysite.com/something/page.php
and finds this script:

Code: Select all

if (!isset($_SESSION['userName']) && !isset($_SESSION['userId']))
{
	header('Location: http://www.mysite.com/login.php');
	exit();
}
Once the user's redirected I expected it to jump directly to mysite.com/login.php, however somehow it only includes the script from login.php but stays in the something folder.
I don't have absolut urls in the login.php script meaning that any images used on that page are broken and won't load, because they have the wrong path.

Is there a quick fix to this problem?

Do I need to change everything to absolut urls or can I do something to phisically jump to mysite.com/login.php?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

does ../login.php work?
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Yeah I tried that too and that gives me a HTTP request error...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

do you have a .htaccess file with anything in it? (in either the root or the 'something' folder?)
B.Murali Krishna
Forum Commoner
Posts: 28
Joined: Fri May 12, 2006 2:05 am
Location: Hyderabad,India
Contact:

Re: header() question

Post by B.Murali Krishna »

use ob_start(); at starting of that page to initiate header's.

Regards
Murali
speedy33417 wrote:I'm getting stuck with a header issue. I have a login page at mysite.com/login.php and I'd like to direct any unathorized access to this page from any part of the site.
My problem is if the user tries to see mysite.com/something/page.php
and finds this script:

Code: Select all

if (!isset($_SESSION['userName']) && !isset($_SESSION['userId']))
{
	header('Location: http://www.mysite.com/login.php');
	exit();
}
Once the user's redirected I expected it to jump directly to mysite.com/login.php, however somehow it only includes the script from login.php but stays in the something folder.
I don't have absolut urls in the login.php script meaning that any images used on that page are broken and won't load, because they have the wrong path.

Is there a quick fix to this problem?

Do I need to change everything to absolut urls or can I do something to phisically jump to mysite.com/login.php?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Output buffering is not a solution to header() problems.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Try using $_SERVER['DOCUMENT_ROOT'] if you having a problem with which folder your being redirected in.
Post Reply