By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

Problem with Redirect in PHP

P: 25
Hi , I am having some problem with redirecting the webpage after authenticating the user login. I have pasted the code below. My main body is after the last line of the code. Everything is working fine except the redirection one. I dont know whats wrong with my code

<?php
session_start();
require_once 'functions.php';
$UserName = $_POST['UserName'];
$Password = $_POST['Password'];
if ($_POST){
$error = login_check($_POST);
if (trim ($error)=="")
{
$accesslevel = accessLevel($UserName);
?>
<?php
if ($accesslevel == "admin"){
//echo "this is admin";
$_SESSION["userid"] = login($_POST);
header('Location: adminlogin.php');
exit();
}
else if ($accesslevel == "user") {
//echo "this is user";
$_SESSION["userid"] = login($_POST);
header('Location: userlogin.php');
exit();
}
}
else {
print "Error :$error";
}
}
?>
<BODY>
<FORM id=form1 name=loginform method=post>
<TABLE align=center>
<TBODY>
<TR>
Feb 15 '07 #1
Share this Question
Share on Google+
15 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Please read the Posting Guidelines before you post in this forum!.

Read the part about enclosing code within code or php tags!!

moderator
Feb 15 '07 #2

P: 99
you can't use relative urls. change them to absolute urls
Feb 15 '07 #3

P: 25
I have actually made the whole authentication code on same page (login.php) and the functions that i used in
this code are in functions.php. Now this time it is showing the
warning :
Warning: Cannot modify header information - headers already sent
by (output started at C:\Program
Files\xampp\htdocs\fselection\login.php:1) in C:\Program
Files\xampp\htdocs\fselection\login.php on line 19
The line 19 is header("Location: http://$host$uri/$extra");

Below is the list of code for login.php.
The Server i am using is SunOS 5.10.

<?php
session_start();
require_once 'functions.php';
$UserName = $_POST['UserName'];
$Password = $_POST['Password'];
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'adminlogin.php';
$loc = "\"http://$host$uri/$extra\"";
if ($_POST){
$error = login_check($_POST);
if (trim ($error)=="")
{
$accesslevel = accessLevel($UserName);
?>
<?php
if ($accesslevel == "admin"){
$_SESSION["userid"] = login($_POST);
header("Location: http://$host$uri/$extra");
exit();
}
else if ($accesslevel == "user") {
//echo "this is user";
$_SESSION["userid"] = login($_POST);
header('Location: userlogin.php');
exit();
}
}
else {
print "Error :$error";
}
}
?>
<BODY>
<FORM id=form1 name=loginform method=post>
<TABLE align=center> ........
***********************Login Form Goes Here **********
nothing php Stuffs on this part
</body>

ANy Idea on whats wrong with my script
Feb 16 '07 #4

Atli
Expert 5K+
P: 5,058
Try putting ob_start() right after your session_start() and
ob_end_flush() at the bottom of your code.

Headers can not be sent after you output anything to the browser.
The ob_start() function prevents the script from sending anything, buffering it untill you call the ob_end_flush(), and then it sends it all at once.
Feb 16 '07 #5

P: 25
Try putting ob_start() right after your session_start() and
ob_end_flush() at the bottom of your code.

Headers can not be sent after you output anything to the browser.
The ob_start() function prevents the script from sending anything, buffering it untill you call the ob_end_flush(), and then it sends it all at once.
Hey thanks Atli, But it is still not working even if kept ob_start() after session_start() and ob_end_flush at the buttom of the code . It is still giving me the same error as Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\fselection\login.php:1).

I dont know where the problem is. Is it because i am including functions.php and using some function to check authentication.

I have no idea why this is not working.
Feb 16 '07 #6

Atli
Expert 5K+
P: 5,058
Could there be a line before you open the php tag?
You could also try putting ob_start before session_start.

I personally try to avoid using the header() function. I try to design my pages to use classes an include so I never leave the index.php
.... but that's just me :)

If you dont mind making your code ugly you could do this:
[PHP]<?php echo "<script language='javascript'> location.href='http://www.google.com'; </script>"; ?>[/PHP]
Tho I personally would never be cought doing that :P
Feb 16 '07 #7

P: 25
Could there be a line before you open the php tag?
You could also try putting ob_start before session_start.

I personally try to avoid using the header() function. I try to design my pages to use classes an include so I never leave the index.php
.... but that's just me :)

If you dont mind making your code ugly you could do this:
[PHP]<?php echo "<script language='javascript'> location.href='http://www.google.com'; </script>"; ?>[/PHP]
Tho I personally would never be cought doing that :P
Thanks again Atli,
i think i better use include or the javascipt that you just gave... And it seems to work too ..SO its a kind of fast and dirty approach may be...whatever...Also one last question if you dont mind. After the user is directed to the correspondng page lets say admin is directed to adminlogin.php page. I have a script where i use inlude and try to stay in the same page. I have a several link where i have used link like adminlogin.php?mode=name and later in the code i have if then statement which says. if ($mode==name) then include ('somefile.php')
this code works fine when i use in my localhost on WIndows machine. But when i upload to the Unix server. it does not does anything . just the url changes to adminlogin.php?mode=name. But the page refreshes and doesnot goes to the included page.
I have absolutely no idea what is going wrong. Any Idea?
Once again thanks a lot for the help.
Feb 16 '07 #8

Atli
Expert 5K+
P: 5,058
Your server is probbly configured differently from your localhost.

When you go to a link like this: www.url.com?var=value
the variable var is passed using the HTTP GET protocol.

If you have register_globals set to on in your php.ini you can access GET and POST variables like any other variables.
New releases of php have this set to off by default for security reasons, and It is reccomended that any online server has this set to off.

You can still access GET and POST variables using
$_POST['var'] or $_GET['var'], or even $_REQUEST['var']

So you should try to change your code like this.

[PHP]
//From
if($mode=="name")

// to
if($_GET['mode'] == "name")
[/PHP]
Feb 16 '07 #9

P: 25
Your server is probbly configured differently from your localhost.

When you go to a link like this: www.url.com?var=value
the variable var is passed using the HTTP GET protocol.

If you have register_globals set to on in your php.ini you can access GET and POST variables like any other variables.
New releases of php have this set to off by default for security reasons, and It is reccomended that any online server has this set to off.

You can still access GET and POST variables using
$_POST['var'] or $_GET['var'], or even $_REQUEST['var']

So you should try to change your code like this.

[PHP]
//From
if($mode=="name")

// to
C
[/PHP]

THanks ATli,
That was really a Great Help... It works now when i changed my code to [PHP] fac_mgmt [/PHP] I was Spending a lot of time on that... You guys are owesome.. I really solved my major problem..
Thanks a lot
Feb 16 '07 #10

Atli
Expert 5K+
P: 5,058
Glad I could help :)
Feb 16 '07 #11

P: 25
Glad I could help :)
Hi Atli,
One more trouble,

When i use the Javascript to Redirect the page [PHP] echo "<script language='javascript'> location.href='$adminAddress'; </script>"; [/PHP]

Every thing is Fine it redirects me. But i dont know why i am not able to pass the Session Variable with that to the other page. Do you have any idea how can i still have a session maintained even if i use the above Javascript to redirect.

Thanks a lot
Feb 16 '07 #12

ronverdonk
Expert 2.5K+
P: 4,258
You don't have to pass the $_SESSION array, it is in global storage for the duration of your browser session and available to your script (provided you have started your scripts with the start_session() command).

Ronald :cool:
Feb 16 '07 #13

Atli
Expert 5K+
P: 5,058
Yes, as Ronald said.
All you have to do is put session_start() at the top of your page
and you can use the $_SESSION array, without passing a thing.
Feb 16 '07 #14

P: 25
Yes, as Ronald said.
All you have to do is put session_start() at the top of your page
and you can use the $_SESSION array, without passing a thing.
THank you very much ...i Fix the problem.. :-)
Feb 16 '07 #15

ronverdonk
Expert 2.5K+
P: 4,258
You are welcome.

Ronald :cool:
Feb 16 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.