PHP goto another web page automatically after login?

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
mikeguan
Forum Newbie
Posts: 2
Joined: Thu Jul 31, 2008 9:59 pm

PHP goto another web page automatically after login?

Post by mikeguan »

I have created the login part, but how do I redirect the user to another web page after the user have login? Any help or examples would appreciated. Thanks.
sHobbsWW
Forum Newbie
Posts: 15
Joined: Wed Jul 16, 2008 10:48 am

Re: PHP goto another web page automatically after login?

Post by sHobbsWW »

Hi mike!

There are a few ways to go about redirection, however the most popular of the two are:

Meta refresh.
301 redirect.


What is the difference? Well I can't speak to much on it because I do not yet know a lot about SEO. But the 301 redirect is more vital when dealing search engines. (Kind of like a permanently moved webpage that still retains its link popularity.)

I will read more up on it, as should you. :)

Anyway, the META refresh:

Code: Select all

<meta http-equiv="refresh" content="3; url=http://www.example.com">
Where content="3 is in seconds

The 301 redirect:

Code: Select all

<?
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.new-url.com" ); 
?>
mikeguan
Forum Newbie
Posts: 2
Joined: Thu Jul 31, 2008 9:59 pm

Re: PHP goto another web page automatically after login?

Post by mikeguan »

Thanks for the quick reply. Unfortunately, I have tried the 2 below already:

1-Meta refesh is in html so I don't think I can have if statements with it.

2-I use the 301 refresh code and it doesn't seem to work at all. I'm not sure if it have somethign to do with the PHP version or where I'm placing the code at?
dwightlathan77
Forum Newbie
Posts: 3
Joined: Thu Jul 31, 2008 11:14 pm

Re: PHP goto another web page automatically after login?

Post by dwightlathan77 »

My understanding is that the redirects should be before any html. You can break the code apart from the html by placing it along with the form validation script. The user would click login, it validates and send them to the content page, or back to the login page.

When you say it's in the html I'm guessing your writing a welcome screen that would acknowledge login. in that event you could just use a permanent link and verify that the session is set when they try to enter the content page.
sHobbsWW
Forum Newbie
Posts: 15
Joined: Wed Jul 16, 2008 10:48 am

Re: PHP goto another web page automatically after login?

Post by sHobbsWW »

Mike,

Code: Select all

 
<head>
<?php
    if (condition is met) {
        echo "<meta http-equiv=\"refresh\" content=\"3; url=http://www.example.com\">";
    } else {
        // do w/e you want
    }
?>
</head>
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: PHP goto another web page automatically after login?

Post by jayshields »

Code: Select all

 
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
 
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
 
Straight from the manual http://uk.php.net/manual/en/function.header.php
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

Re: PHP goto another web page automatically after login?

Post by coder500 »

Hi mikeguan,
The following code worked for me.

Code: Select all

 
print "<script>";
print " self.location='http://www.yoursite/index.php';"; 
print "</script>";
 
This will redirect you to the page 'http://www.yoursite/index.php'.
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP goto another web page automatically after login?

Post by RobertGonzalez »

Keep in mind that the javascript code will only work in browsers that have JS enabled. I would not rely on that. A simple header() with an exit() after it will suffice. And I think the location header directive throws a 301 automatically. If not, you can send that header too before sending the location header.
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

Re: PHP goto another web page automatically after login?

Post by coder500 »

Hi Everah,
I used

Code: Select all

 
<?php
header("Location: http://www.mysite.org");
exit();
?> 
 
But it is not redirecting, just it stays in the same page.
Could you please explain, why it is so ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP goto another web page automatically after login?

Post by RobertGonzalez »

Make sure you have no other output (not even an empty space or line) before the header call. Check your error logs for "Headers already sent" error messages or turn on display_errors to see if there is output being sent before the call.
Post Reply