0

I built a web application using PHP and Mysql. It has a login Section. It is working fine and everything ok on my local server. But when I uploaded this app on live server the redirect function is not working any more. My code:

signin.php:

<form action="do_login.php" method="post">
    <input name="user" type="text" id="user" size="25" placeholder="Username" />
    <input name="password" type="password" id="password"  size="25" placeholder="Password" />
<input name="submit" type="submit" value="Login" class="submit"/>
</form>

do_login.php:

<?php
include 'includes/dbConnect.php';   

$my_user = $_POST['user'];
$my_password = $_POST['password'];

if ($my_user == '' || $my_password == '')
    {
        $myURL = 'error.php?eType=pass';
        header('Location: '.$myURL);
        exit;
    }

$result = mysql_query("SELECT * FROM users where username = '$my_user' and password =  '$my_password'") or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
$get_info = mysql_fetch_row($result);

    if (mysql_num_rows($result) > 0)
    { 
        session_start(); 
        $_SESSION['login_status'] = "yes" ;
        $_SESSION['email'] = $get_info['3'];
        $_SESSION['full_name'] = $get_info['0'];
        $myURL = 'admin.php';
        header('Location: '.$myURL);
    }
else
    {
        $myURL = 'error.php?eType=wrong';
        header('Location: '.$myURL);
        exit;
    }
?>

When a user login successfully the browser takes to do_login.php and displays a blank page that does not redirect to admin.php on live server but on localhost it redirect to admin.php. When I manually go to admin.php, which has restrictions so that only logged in users can access it, I can access it that means I have been logged in and works fine.

Where is the mistake?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Majid Ali
  • 31
  • 3
  • 11
  • 1
    Do not use deprecated `mysql_*` API. Use `mysqli_*` or `pdo` – Jens Mar 09 '15 at 06:49
  • 1
    Turn on error reporting - `ini_set('display_errors', 1); error_reporting(E_ALL);`. I'm almost certain you'll get a `"Headers already sent"` error. – Darren Mar 09 '15 at 06:50
  • Take a look to your server logfiles to find out if there are errors. – Jens Mar 09 '15 at 06:50
  • @Darren can u plz point the mistake in my code? – Majid Ali Mar 09 '15 at 06:54
  • 1
    Can you do what I said @MajidAli and tell me what errors show up. – Darren Mar 09 '15 at 06:55
  • 1
    People who visit here are in principle willing to help. So instead of begging (in the question and comments) update your question as requested, because it is incomplete. When updating never use **Edit** or **Update**, we can see from the system edit history what has changed if necessary. Far more important is an easy readable, coherent, question, without chit-chat. – Anthon Mar 09 '15 at 07:22
  • are you sure your `dbConnect.php` doesn't have any `echo` statement. may be `DB connection` not successful on live server – Mamun Sardar Mar 09 '15 at 07:27
  • Try these suggestions: 1. Put `session_start();` in the beginning of script, just after opening PHP tag. 2. Put `exit();` after `header()`. After implementing these, try running your code & share your comments. – Apul Gupta Mar 09 '15 at 07:43

2 Answers2

1

Wrap your php code in ob_*

<?php

ob_start();

//Rest of your code

ob_end_flush();
?>

Updated: Here is the reason i suggested ob_* functions. I copied it from brian.moonspot.net

How ob_start works

So, how does ob_start help? The ob in ob_start stands for output buffering. ob_start will buffer the output (HTML) until the page is completely done. Once the page is completely done, the headers are sent and then the output is sent. This means any calls to setcookie or the header function will not cause an error and will be sent to the browser properly. You do need to call ob_start before any output occurs. If you start output, it is too late.

Raheel
  • 8,716
  • 9
  • 60
  • 102
0

Use full website Site Url before file like :-

 $site_url = "www.example.com";
 $myURL = $site_url.'/admin.php';
 header('Location: '.$myURL);