logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Redirect to another webpage in PHP?
Author
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Tue 31 Oct '06 16:15    Post subject: Redirect to another webpage in PHP? Reply with quote

Hi,

How would I redirect to a different webpage after logging into my website?

Actually, when the user logs onto my website with an incorrect user id and/or password, instead of going to the main page, it must redirect to an error page.

How would I do this in PHP?

Thanks

Quote:
Every man has to go through hell...to reach his paradise
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Tue 31 Oct '06 18:00    Post subject: Reply with quote

Code:

/* Redirect to another page in the same folder */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
if($login=="1"){
   $extra = 'page.php';
}
else
{
   $extra = 'error.php';
}
header("Location: http://$host$uri/$extra");
exit;


hope that helps

Instead of the page.htm you could also include a page and put the header and exit under the error.php Wink
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Tue 31 Oct '06 20:01    Post subject: Reply with quote

Just remember that you need to use header( 'location... prior to kicking out other content type headers, or the re-direction wont happen.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Tue 31 Oct '06 20:21    Post subject: Reply with quote

Brian wrote:
Just remember that you need to use header( 'location... prior to kicking out other content type headers, or the re-direction wont happen.

What do you mean?

this example is taken from php.net and it works great I use it in several pages on windows and *nix systems.
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Tue 31 Oct '06 20:38    Post subject: Reply with quote

If you have already sent, for example ...

Code:
header( 'content-type: text/html' );

echo 'test';

header( 'location: someurl.com' );

exit;


... it wont redirect.

I use a little more robust solution that takes into account not using globals, using E_ERROR = ALL, and so on.

Code:
<?php

/*
 at the top of the page before any other headers are sent or any echo / print statemetns ... and so on ...
*/

// here I check if the var is passed or not, I assume you have
// E_ERROR set to all or even notify, I like clean code
$pass = ( isset( $_POST['pass'] ) ) ? $_POST['pass'] : false;
$user = ( isset( $_POST['user'] ) ) ? $_POST['user'] : false;

##########################################
#
# do your validation of submitted
# form data here
#
# assume for example you return
# $valid = 1; for valid login
#
##########################################

// is it a valid login
switch( $valid ) {

   case '1':
      $loc = $your_logged_in_url;
   break;

   default:
   case( $valid != 1 ):
      $loc = $your_not_logged_in_url;
   break;

}

header( 'location: ' . $loc );

// From here you could put in some HTML to help if the header
// redirect fails.  Use Meta Refresh and a clickable link as fail
// overs in case the redirect fails for any reason.

exit;

?>


Last edited by Brian on Tue 31 Oct '06 21:51; edited 1 time in total
Back to top
kr33



Joined: 19 Sep 2006
Posts: 64
Location: South Africa

PostPosted: Tue 31 Oct '06 21:42    Post subject: Reply with quote

Thanks guys, I'm sure it will help.

I'm developing a major website for this company i'm working with and i'm sorta learning a few things over and above my current PHP knowledge.

I got php working on Win Server 2k3, using cgi.

And guys don't fight, we're all part of the Elite scripters or coders around the world, although i'm new to php, I've learnt a HELL of a lot from you guys and mostly by trial and error.

So I hope to contribute more soon and become one of the truly Elite Cool

Thanks
Back to top


Reply to topic   Topic: Redirect to another webpage in PHP? View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner