-1

Ive read pretty much alot of post about this but seems like its not helping me.

I have been working an a testing local server and everything has been working swimmingly, but when I uploaded it to the web the php is suddenlly not working properly. I am connected to the db and all. the main issues I have is with the header (as far as I know).

I have been shaving bits of code and simplifying it to narrow down the problem and came to the conclusion that it was the header:

<?php
require 'admin/Connections/Connections.php';
session_destroy();
if(isset($_POST['Login'])){

$UN = $_POST['Username'];
$PW = $_POST['password1'];

$result = mysql_query("select * from admin where Username='$UN'");

$row = mysql_fetch_array($result);
echo $row['Password'];
echo $_POST['password1'];

if($PW == $row['Password']){

header('Location: admin/home.php');

}else{

    $_SESSION["LoginFail"] = "Yes";
}
}
?>

the echo spits out both $post and $row as the same value so I know it should execute the header, but it just refreshes the page with the added echos.

I tried replacing the url with something like http://www.google.com/ but that takes me to a blank page with the echos. what am I missing? if it is something obvious you have permission to trash me as long as you give me an answer...

Eric
  • 35
  • 1
  • 6
  • 6
    header() won't work if you print anything on the output. you print out two variables before the header(). – DNReNTi Oct 14 '15 at 12:25
  • If you have echo'd something then the header **will not do anything** as you have already sent the headers to the browser, you can't modify the headers after this point. – Geoff Atkins Oct 14 '15 at 12:25
  • 1
    A good 1st step with any debugging is to turn on error reporting. A a side note, you appear to be using depreciated sql functions, using user input directly in an sql query, and saving plaintext passwords in the DB, which are all serious issues of their own – Steve Oct 14 '15 at 12:27
  • Thanks for all your replies and the reoccurring reply seems to be that echo cannot be put before a header.. but could someone care to explain why this was all possible on my local tests but suddenly a rule on a webserver? if this is the case I will have to go through all my files and rewrite them... – Eric Oct 14 '15 at 12:41
  • Have you *really* read this: http://stackoverflow.com/a/8028987/476? – deceze Oct 14 '15 at 13:00

5 Answers5

2

Echoing output before you use header() will stop the header from working. From the PHP documentation for header():

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

At minimum, the header() call should precede the echo calls you have in your code. If that doesn't work, check to see if any output is happening in the require()ed file at the top of your code.

enkrates
  • 626
  • 1
  • 6
  • 17
1

You are outputting HTML with echo. Therefore, header will not redirect.

Solutions:

1) Add ob_start() at the page start. This will store all your output to buffer and thus your page will not have any HTML output, redirection will occur.

2) Comment the code of echos.

3) Use javascript redirect:

echo "<script>window.location.href='admin/home.php';</script>";

This will work irrespective of what is output on the page.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

You cant use PHP's header function after an echo, just put it before your echo, then it should work.

nameless
  • 1,483
  • 5
  • 32
  • 78
0

You cannot use header() function if you make any output to the client. Sometimes it's referred as "headers already sent" notice.

Also, consider moving alway from mysql_* functions as they're deprecated and won't be supported anymore. Try mysqli.

Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63
0

First, remove your echo statements or comment them. when you are taking the user to some other page why the hell are you echoing anything here? To show to whom?

and write ob_start(); at the top of your PHP file i.e:

 <?php
ob_start();
--------the rest of your code---
iaaphpd13
  • 29
  • 1
  • 2
  • 9