0

Using the following condition for redirecting users to login.php is not working.

<?php 
if(userid()=='') {
?>
header ('location: login.php');

Output in webpage is displayed like header ('location: login.php'); and no redirection is happening.

Sorry i am new to PHP please help me out if i am wrong.

Manju
  • 11
  • 1

6 Answers6

4

Simple Typo:

Change to:

<?php 
if(userid()=='') {
    header ('location: login.php');
    exit();
    // Your code.
}
?>

PHP, JavaScript and HTML codes can be embedded in each other without hassle in PHP file.

But, PHP code must be enclosed within <?php and ?> OR <? and ?>

JavaScript code must be enclosed within <script> and </script>.

Any code that is not enclosed within above two is considered as Plain HTML.

Sagar Guhe
  • 1,041
  • 1
  • 11
  • 35
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • close the curly braces? – low_rents Apr 20 '16 at 06:25
  • 3
    I think it's worth noting that calling `exit()` after your `header(...)` call is a good idea, as otherwise PHP will happily continue processing whatever else might be part of that particular script. – kungphu Apr 20 '16 at 06:25
  • @kungphu To avoid having to do that, you can put the rest of your code in an `else`-clause instead. – klaar Apr 20 '16 at 06:26
  • And only one dubble quote? – Andreas Apr 20 '16 at 06:26
  • @klaar I'd call that a matter of preference; I personally would rather explicitly call `exit()` than have to wrap the entire rest of my script in an otherwise-unnecessary conditional. Either, though, is valid (unless your script has been included as part of another, in which case this suggestion won't effectively stop execution). – kungphu Apr 20 '16 at 06:27
  • Hi all, have updated the code accordingly. – Pupil Apr 20 '16 at 06:28
  • @kungphu That is indeed a matter of preference, because I'd like to see at a glance that code is only executed if a condition has (or not) been fullfilled. If it's not within braces, I'd assume that it will run no matter what. I don't like exit statements that you explicitly have to look for to know they are there; as for an if-else with curly braces you can see at a glance that it is subject to condition testing. But again, that's personal. – klaar Apr 20 '16 at 06:36
1

Your PHP syntax is not correct:

<?php 
   if(userid()=='') {

     header ('Location: login.php'); //header should be within if loop

   } //you missing closing brace

?>

Learn the basic of PHP condition

Meathanjay
  • 2,023
  • 1
  • 18
  • 24
1

The right Syntax would be:

header( "Location: login.php" ); die;  

You also forgot to close the { in your if statement, so you have to add it after the statement above

1

try this

<?php   if(userid()=='') {

 header ('Location: login.php');  exit();   }  ?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

Simply Change this

<?php 
if(!userid()) {
header ('location: login.php');
}
?>
Ravi Kumar
  • 443
  • 3
  • 10
0

Solution-1

start php tag

if( userid()=='' ) {

header ('location: login.php');

}

end php tag

Solution-2

Omit the ending php tag (?>) in any php file.

start php tag

if( userid()=='' ) {

header ('location: login.php');

}

do not end php tag

masuduzzaman
  • 113
  • 1
  • 5