tags:

views:

51

answers:

1

Hi ,

Basically I will tell you what I want to do.

1: If a user is not logged in I want them to be re-directed back to the login page.

2: If a user is Logged in and they submitted a form I want them to be directed to the next page.

I have tried it with meta refresh but I can only seem to get it working with one or the other.

Can you please advise what the best way to do this would be ?

the code I am using at the moment is

<meta http-equiv="refresh" content="0;index.php">  
<meta http-equiv="refresh" name="myidentifier" content="0;mystats.php">

Thanks

+5  A: 

Use http headers instead. For example:

session_start();

//Redirect when user is not logged
if($_SESSION['logged'] != 1)
{
  header("Location: http://redirect.here.com/login.php");
  exit(0);
}

//Redirect when user sent form
if((isset($_POST['sent']))&&($_SESSION['logged']==1))
{
  header("Location: http://redirect.here.com/nextpage.php");
  exit(0);
}

Don't forget to set $_SESSION['logged']=1 after successful login. There are more methods of detecting that user sent form, but I prefer placing hidden input field with name="sent" to each form.

Petr Peller
Ahh ok then can I use a meta refresh for the other part ?
Oliver Bayes-Shelton
I wouldn't use meta tags for redirect purpose when you can use http headers instead. I think it's always better to avoid using meta tags when there is http header equivalent.
Petr Peller
You don't need the meta refresh (which is bad practice alltogether).
Boldewyn
Ok Thanks very much
Oliver Bayes-Shelton
The else-block code can be in the main scope of the function. Because the if-block code exits, it is a guard clause.
Ewan Todd
You are right Ewan, but I think it's more human readable with 'else'.I edited the answer to cover both parts and deleted the 'else' block anyway.
Petr Peller
I am getting the following error "Warning: Cannot modify header information - headers already sent"
Oliver Bayes-Shelton
You must make sure that you didn't send any output before calling header() function. Note that even space before '<?php' tag can be considered as output.
Petr Peller