Solved

redirect to another page

Posted on 2008-10-15
20
294 Views
Last Modified: 2010-04-21
i have some PHP script that i need to authenticate a user and if a session is empty to redirect the user to another page



HERE IS MY CODE
<?php

session_start(); 

//if the session is registered to a valid user then show content 

if ($_SESSION["Username"])

 { 

print "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"].

" $surname<br>";

echo date("l, F d, Y h:i" ,time());

echo "";

echo "  Your IP Address ";

echo $_SERVER["REMOTE_ADDR"];

} 

//not registered session 

else

{ 

echo "What are you doing here";

} 

//var_dump($_SESSION);

?>

Open in new window

0
Comment
Question by:Chris Jones
  • 10
  • 6
  • 4
20 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22722525
try:
<?php
session_start(); 
//if the session is registered to a valid user then show content 
if ($_SESSION["Username"])
 { 
print "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"].
" $surname<br>";
echo date("l, F d, Y h:i" ,time());
echo "";
echo "  Your IP Address ";
echo $_SERVER["REMOTE_ADDR"];
} 
//not registered session 
else
{ 
	header("Location: otherpage.php");
	exit;
} 
//var_dump($_SESSION);
?>

Open in new window

0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22722645
still stays at the same page ?
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 22722753
hielo's suggestion is fine, but only if you have no output (not even a blank space) generated by php before line 16.
Make sure there is no output at all before that.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22722956
change:
if ($_SESSION["Username"])

to:
if ( isset($_SESSION["Username"]) && !empty($_SESSION['Username']) )
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22723785
AlexanderR:

so will this cause the problem
<?php

session_start(); 

//if the session is registered to a valid user then show content 

if ($_SESSION["Username"])

 { 

print "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"].

" $surname<br>";

echo date("l, F d, Y h:i" ,time());

echo "";

echo "  Your IP Address ";

echo $_SERVER["REMOTE_ADDR"];

} 

//not registered session 

else

{ 

echo "What are you doing here";

	header("Location: members.php");

} 

//var_dump($_SESSION);

?>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22723925
>>so will this cause the problem
Not sure how that is different from your original code, which you already know does not work. You need to make sure the SESSION variable is valid or else redirect:
<?php

session_start();
 

//if the session is registered to a valid user then show content 

if( isset($_SESSION["Username"]) && !empty($_SESSION["Username"]) )

{ 

	echo "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"]." $surname<br>";

	echo date("l, F d, Y h:i" ,time());

	echo "";

	echo "  Your IP Address ";

	echo $_SERVER["REMOTE_ADDR"];

} 

//not registered session 

else

{ 

	header("Location: otherpage.php");

	exit;

} 

?>

Open in new window

0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22723964
sorry i mean

AlexanderR:
POSTED "hielo's suggestion is fine, but only if you have no output (not even a blank space) generated by php before line 16.
Make sure there is no output at all before that."
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22723980
the session varables are valid i am printing the user name ou to the screen and i also did a
var_dump($_SESSION);
witch gives me all my values
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22724085
hielo:

this did not work


if ( isset($_SESSION["Username"]) && !empty($_SESSION['Username']) )

 { 

print "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"].

" $surname<br>";

echo date("l, F d, Y h:i" ,time());

echo "";

echo "  Your IP Address ";

echo $_SERVER["REMOTE_ADDR"];

} 

//not registered session 

else

{ 

echo "What are you doing here";

	header("Location: members.php");

} 

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22724302
If your page is to redirect, you CANNOT send output to the browser. So this will NOT work:
echo "What are you doing here";
header("Location: members.php");

because the echo sends output to the browser so the redirection will be canceled. Get rid of the echo statement.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Chris Jones
ID: 22724469
ok this just gives me a blank page and does not redirect to another page
0
 
LVL 82

Expert Comment

by:hielo
ID: 22724564
try using the full url to the page and follow it with exit:
...
else
{
 header("Location: http://www.yoursite.com/some/directory/members.php");
 exit;
}
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22724784
this still did not work  it bombs on the if statement it only shows half the screen
0
 
LVL 82

Expert Comment

by:hielo
ID: 22724898
>>it bombs on the if statement it
What is "bombs" to you? Are saying it is going into the if clause but having problems there?

Try changing:
$HTTP_SESSION_VARS["initial"]

to:
$_SESSION["initial"]
0
 
LVL 11

Accepted Solution

by:
AlexanderR earned 500 total points
ID: 22726974
I suggest we isolate the problem to be sure if its something with SESSION variables logic or having an output before the header.  This is an alternative way to redirect using javascript.  If it works, then there is a problem with an output before header function.  If it does not, then its with SESSION.


<?php

session_start(); 

//if the session is registered to a valid user then show content 

if ($_SESSION["Username"])

 { 

print "Welcome      ".$_SESSION["Username"]."".$HTTP_SESSION_VARS["initial"].

" $surname<br>";

echo date("l, F d, Y h:i" ,time());

echo "";

echo "  Your IP Address ";

echo $_SERVER["REMOTE_ADDR"];

} 

//not registered session 

else

{ 

echo '<script language="text/javascript">

window.location = "members.php";

</script>';

} 

//var_dump($_SESSION);

?>

Open in new window

0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22727361
this did not fix the problem it just gave me a blank page it did not redirect it allowed me in the page
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 22727409
so sorry for this round about discussion.

I made a slight mistake
echo '<script language="text/javascript">
should be
echo '<script type="text/javascript">
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 22730798
ok this worked great
0
 
LVL 1

Author Closing Comment

by:Chris Jones
ID: 31506354
i finally got help with this problem great help thanks guys
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 22735328
I am glad that my suggestion worked, but you should NOT use it for something as serious as validating a user, because all they have to do is turn their JS off and they are in.

This was just a trouble shooting step.  Because my example worked, we know SESSION variables are OK and if statements are OK. The problem is now that you have some sort of output going before "header" function.

I strongly suggest that you thoroughly check all of your code to make sure that there is not even a blank space escaping the server before "header" is called.  There should be nothing, no spaces, between or after ?> signs.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now