header() function in php : No any redirection!

Posted by jasmine on Stack Overflow See other posts from Stack Overflow or by jasmine
Published on 2010-03-24T16:42:53Z Indexed on 2010/03/24 16:53 UTC
Read the original article Hit count: 205

Filed under:

I have written a very very very simple!! script in php. header redirection not working. 1- encoding : UTF-8 without BOM 2- with adding ob_start() the problem is countiueing. What is wrong in my code;

login.php:

<?php  session_start();
   require_once("funcs.php"); 
   db_connection();
   $username = $_POST['username'];
$password = $_POST['pwd'];
$submit = $_POST['login'];
  if($submit){
if (!filled_out($_POST)) {
 echo "please fill all fields";
}
 else{
   $query = "SELECT * FROM users WHERE username ='{$username}' AND password      ='{$password}'";
 $result = mysql_query($query);
  if(mysql_num_rows($result) == 1){
  $found_user = mysql_fetch_array($result);
  $_SESSION['id']  = $found_user['id'];
  $_SESSION['username'] = $found_user['username'];
  $_SESSION['password'] = $found_user['password'];
  setcookie(session_name(), '', time()+86400, '/');
  header("Location: tst.php");
  }
  else{
  echo "incorrect username or password";
  }

                            }
                                 }     


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org     /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

 <form id="form1" name="form1" method="post" action="">
 <p>
 <label for="username">Username:</label>
 <input type="text" name="username" id="username" />
 </p>
 <p>
 <label for="textfield">Password</label>
 <input type="password" name="pwd" id="pwd" />
 </p>
 <p>
  <input name="login" type="submit" id="login" value="Log in" />
  </p>
 </form>
 </body>
 </html>

 <?php  db_disconnect();?>

and tst.php:

                   <?php session_start();  
               require_once("funcs.php");  
                if (!isset($_SESSION['id'])){
    header("Location : login.php");
   }
                     ?>
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>

        <body>
        <table id="structure">
    <tr>
 <td id="navigation">&nbsp;

 </td>
 <td id="page">
 <?php


   echo "welcome"."". $_SESSION['username'];

   ?>  
 </td>
   </tr>
    </table>
    </body>
    </html>
     <?php db_disconnect();?>

wthit oppening tst.php directly, header() doesnot redirect to login.php

© Stack Overflow or respective owner

Related posts about php