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?

8
  • 1
    Do not use deprecated mysql_* API. Use mysqli_* or pdo Commented Mar 9, 2015 at 6: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. Commented Mar 9, 2015 at 6:50
  • Take a look to your server logfiles to find out if there are errors. Commented Mar 9, 2015 at 6:50
  • 1
    Can you do what I said @MajidAli and tell me what errors show up. Commented Mar 9, 2015 at 6: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. Commented Mar 9, 2015 at 7:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should explain why he should wrap it in ob_* functions, not just suggest it.
0

Use full website Site Url before file like :-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.