Forum Moderators: coopster

Message Too Old, No Replies

redirect after login

how do you redirect to a base page after someone has logged in?

         

appleg5ive

7:27 pm on May 1, 2004 (gmt 0)

10+ Year Member



After finally getting my registration and login scripts sorted, I realised that I needed to redirect to a base page for each user, is there a simple-ish way of scripting an automatic redirect should a user log in successfully?

WhosAWhata

9:24 pm on May 1, 2004 (gmt 0)

10+ Year Member



where do you need them to go?
if you haven't echo'ed anything yet, you could use a header

$page = "users/$user.html"; // replace with you page

header("Location:$page");

or, you could use a
echo "<body onload=\"window.location='$page'\">";

or

echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=$page\">";

or of course you could
include("$page");

as you can see, there are many ways using PHP or HTML to redirect

appleg5ive

5:10 pm on May 2, 2004 (gmt 0)

10+ Year Member



where abouts do i put it? it doesn't redirect if i put it within the log in script

ergophobe

6:16 pm on May 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You put it in whichever page your login form submits to (as per the "action" attribute in the form).

appleg5ive

3:48 pm on May 4, 2004 (gmt 0)

10+ Year Member



But I need to ensure that the user is logged in correctly before it redirects, I can make it redirect, but with no certainty that the user is logged in correctly.

jatar_k

5:24 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well normally

1. user fills login form
2. user submits form
3. script submitted to authenticates user entered information
4. once auth is done redirect to
a. login failure page
b. logged in page

so it would be the pass/fail part of your login

ergophobe

5:31 pm on May 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To follow up on Jatar_K,

Once logged in, but before redirecting, you may want to store some login info in a session. Then in your "config" file (any file that gets included in the top of every page) you check for a valid login.

That's one possibility anyway.

Tom

appleg5ive

6:11 pm on May 4, 2004 (gmt 0)

10+ Year Member



So how do I do this (sorry, I'm a bit of a novice)?

jatar_k

6:21 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about describing the logic of the login and maybe we can help.

What happens at the end of the login?
Where does it presently send them?

does it just include a page or have output itself?

appleg5ive

6:43 pm on May 4, 2004 (gmt 0)

10+ Year Member



<?
if($user && $pass) {
if($logged_in_user == $user) {
echo $user.",you are already logged in.";
exit;
}
$db = mysql_connect("*********", "*********", "********");
mysql_select_db("users", $db);
$result = mysql_query("SELECT * FROM users WHERE name = '".$user."' AND password = PASSWORD('".$pass."')");
if(!$result) {
echo "Sorry, there has been a technical error.";
exit;
}
if(mysql_num_rows($result) > 0) {
$logged_in_user = $user;
session_register("logged_in_user");
}
}

?>

Is the login script, which is on a page of it's own, then I use

$page = "centre.php";
echo "<body onload=\"window.location='$page'\">";

As the forwarding script, but if I put it anywhere except on it's own after the login script, it does not work.

jatar_k

6:55 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so once you finish the login you use this for success

if(mysql_num_rows($result) > 0) { // this indicates success
$logged_in_user = $user;
session_register("logged_in_user");
$page = "centre.php";
} else { // this indicates login error
$page = "loginerror.php";
}
header [ca.php.net]("Location: $page");

that should work, though both of those pages would have to be located in the site root. Otherwise the paths will have to adjusted.

appleg5ive

7:01 pm on May 4, 2004 (gmt 0)

10+ Year Member



thanks, i'll give it a try

jatar_k

7:06 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



remember that there can be absolutely no output to the browser before calling header.

even a space or an empty line at the top of the file before the <? can cause it to error out.

appleg5ive

7:10 pm on May 4, 2004 (gmt 0)

10+ Year Member



Okay, so this is what I have now;

<?
if($user && $pass) {
if($logged_in_user == $user) {
echo $user.",you are already logged in.";
exit;
}
$db = mysql_connect("*******", "*****", "*******");
mysql_select_db("users", $db);
$result = mysql_query("SELECT * FROM users WHERE name = '".$user."' AND password = PASSWORD('".$pass."')");
if(!$result) {
echo "Sorry, there has been a technical error.";
exit;
}
if(mysql_num_rows($result) > 0) {
$logged_in_user = $user;
session_register("logged_in_user");
$page = "centre.php";
} else {
$page = "loginerror.php";
}


}
echo "<body onload=\"window.location='$page'\">";
?>

But it is keeps trying to reload the same page (login.php) rather than continuing to the base page.

jatar_k

7:11 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try using the header function instead of the body onload stuff

WhosAWhata

2:05 am on May 5, 2004 (gmt 0)

10+ Year Member



header() is a much better way to go ( i was just trying to present all of the alternatives i could think of)

appleg5ive

3:26 pm on May 5, 2004 (gmt 0)

10+ Year Member



This is what I have currently, but I keep getting it retuerning saying that pages starting with "(null)" are not supported by the browser. Any ideas?

<?
if($user && $pass) {
if($logged_in_user == $user) {
echo $user.",you are already logged in.";
exit;
}
$db = mysql_connect("*", "*", "*");
mysql_select_db("users", $db);
$result = mysql_query("SELECT * FROM users WHERE name = '".$user."' AND password = PASSWORD('".$pass."')");
if(!$result) {
echo "Sorry, there has been a technical error.";
exit;
}
if(mysql_num_rows($result) > 0) {
$logged_in_user = $user;
session_register("logged_in_user");
$page = "centre.php";
} else {
$page = "loginerror.php";
}

}

header("Location: $page");
?>

coopster

5:38 pm on May 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try moving your header statement inside your if loop...

That's not going to fix things, though, unless you are handling the error (no user, no password entered) somewhere else inside this script.

StupidScript

8:56 pm on May 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like this:

if(mysql_num_rows($result) > 0) {
$logged_in_user = $user;
session_register("logged_in_user");
header("Location: centre.php");
} else {
header("Location: loginerror.php");
}

StupidScript

9:00 pm on May 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW: It looks like your error (pages starting with "(null)") might be generated by an uninitialized variable. $page is only assigned a value if ($user && $pass). You might want to add:

if ($user && $pass) {
...yaddayadda...
} else {
$page = "login.php";
}

...if you will be continuing to use the secondary redirection rather than including the redirection in the qualifying login routine.

StupidScript

9:04 pm on May 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry...I left out an important element:

if(mysql_num_rows($result) > 0) {
$logged_in_user = $user;
session_register("logged_in_user");
header("Location: centre.php");
exit;
} else {
header("Location: loginerror.php");
exit;
}

(Without exiting from the routine after redirecting, PHP will continue to "walk" the page, possibly messing with something else.)

:)