0

i have a login page that allow user to enter email and password then submit and the system check if data is correct it display the profile page and if not it display a message inform the user that the data are not correct .

but the problem is that if i put header("Location:profile.php"); the system do not work but if i echo a message that inform user that the data are correct the browser display this message without any problem

login.php

<?php
session_start();
ob_start();
error_reporting(E_ALL);
require_once('include/connect.php');
//$message = ""; 
if(!empty($_POST['email']))
{

$email = $_POST['email'];
$pass = $_POST['pass'];

$email = strip_tags($email);
$pass = strip_tags($pass);
$email = mysql_real_escape_string($email);
$pass = mysql_real_escape_string($pass);
//$pass = md5($pass);

$sql=mysql_query( "SELECT user_id, email_address, first_name FROM user WHERE email_address='$email'AND password='$pass'LIMIT 1") or die("error in user table");
$login_check = mysql_num_rows($sql);

  if($login_check > 0)
  {
      $row = mysql_fetch_array($sql);

          $id = $row['user_id'];
          $_SESSION['user_id'] = $id;

          $firstname = $row['first_name'];
          $_SESSION['first_name']= $firstname;

          $email = $row['email_address'];
          $_SESSION['email_address']= $email;

          mysql_query("UPDATE user SET last_log_date=now() WHERE user_id='$id'");

        //$message = "correct email and passworddd!!";  
          header("Location:profile.php");
          exit();     
  }//close if 
  else
  {
      //$message = "incorrect Email or Password!!";
      //exit();
  }
}//close if

?>  

profile.php

<?php
session_start();
 require_once('include/connect.php'); 


if(isset($_GET['user_id']))
{
    $id=$_GET['user_id'];
    var_dump($id);

}
elseif(isset($_SESSION['user_id']))
{
    $id= $_SESSION['user_id'];
}

else
{
    print "Important  data  are missing";
    print_r($_SESSION);
    exit();

}

$sql = mysql_query("SELECT * FROM user  WHERE user_id='$id'") or die(mysql_error());
$row = mysql_fetch_array($sql);

   $firstname=$row['first_name'];
   $lastname=$row['last_name'];
   $birth_date=$row['birth_date'];
   $registered_date=$row['registered_date'];
   //***************for upload img*****************//
   $check_pic="members/$id/image01.jpg";
   $default_pic="members/0/image01.jpg";
   if(file_exists($check_pic))
   {
       $user_pic="<img src=\"$check_pic\"width=\"100px\"/>";
   }
   else
   {
       $user_pic="<img src=\"$default_pic\">";
   }
   echo $id, $firstname, $birth_date;
?>

4 Answers4

0

use ob_end_flush() before php close tag ?>

Amir
  • 4,089
  • 4
  • 16
  • 28
0

you can use javascript there to redirect on profile page. because if any small mistake like printing before or any space php header() function can cause some problem.. so better user javascript there.

check with bellow code

?>
<script>
window.location.href="profile.php";
</script>
<?
Jaydipsinh
  • 491
  • 1
  • 9
  • 27
  • just comment your headerfunction and copy this code and paste there.. and run your code. – Jaydipsinh May 08 '13 at 05:27
  • I think the better od actual way is output buffering .... not js ... how ever it will work to ..if js is not disabled – NullPoiиteя May 08 '13 at 05:31
  • yes you are right but its a quick way because if we have error in php header, then its very difficult to find and also its take lots of time to check. – Jaydipsinh May 08 '13 at 05:35
0

I have doubt in this line of profile.php

require_once('include/connect.php');

If path of profile.php is faulty, you have to change things in your

header(Location: "xyz/profile.php");

Please check that this relative path is correct!

Mercurial
  • 3,615
  • 5
  • 27
  • 52
0

Try this.

<?php
session_start();
ob_start();
error_reporting(E_ALL);
require_once('include/connect.php');
//$message = ""; 
if(isset($_POST['email']))
{

 $email = $_POST['email'];
 $pass = $_POST['pass'];

 $email1 = mysql_real_escape_string(strip_tags($email));
 $pass1 = mysql_real_escape_string(strip_tags($pass));
 //$pass = md5($pass);

 $sql = mysql_query("SELECT user_id, email_address, first_name FROM user WHERE email_address='$email1' AND password='$pass1' LIMIT 1") or die("error in user table");
 $login_check = mysql_fetch_assoc($sql)

if($login_check)
{
   $row = $login_check;

      $id = $row['user_id'];
      $_SESSION['user_id'] = $id;

      $firstname = $row['first_name'];
      $_SESSION['first_name']= $firstname;

      $email = $row['email_address'];
      $_SESSION['email_address']= $email;

      mysql_query("UPDATE user SET last_log_date=now() WHERE user_id='$id'");

    //$message = "correct email and passworddd!!";  
      header("Location: profile.php");
      exit();     
}//close if 
else
{
  //$message = "incorrect Email or Password!!";
  //exit();
}
}//close if

?>  
Bharat Chodvadiya
  • 1,644
  • 4
  • 20
  • 31