My if else statement working on my local server but not on my live server?

Hello,

I have a if{} else{} piece of code and a redirect function. The redirect function isn't working when hosted on my live server but works on my local server.

The cut down code is as follows:

if (mysqli_num_rows($result) > 0) 
 
	 { 		   		
     
	 echo "greater than zero";
	 
	 } 
 
  else {
    
     //echo "less than zero";
     header('Location: www.orangojapan.com/no_results.php');
     //redirect_to('no_results.php');
     }
  ?> 

Open in new window


I originally was using 'redirect_to('no_results.php');' - I had defined this function as follows:
function redirect_to($location) {	//REDIRCTS TO ANOTHER LOCATION - CAN BE USED IF WRONG PARAMETERS ARE PASSED AT LONG IN ETC.
  header("Location: " . $location); // INFORMATION IS SENT IN THE HEADER
  exit;
  }

Open in new window



This didn't work - no error message, but just no redirect / or as if the else statement was not there. To test, I then replaced  it with the php header(location) function  but this also didn't redirect as hoped. To check if the if / else statement was working I replaced the original code with echo statements. This seemed to work, so I guess the problem is with my functions?

Any thoughts or advice on this. I'm a bit puzzled.

Thanks.
AdamTrying to learn phpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
The most common cause for 'header' to not work is that you have sent some content before that statement.  Headers and cookies have to be sent before anything else.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
Dave is right, also since error reporting in production typically is configured to be silent (showing errors is helpful for debugging but also reveals weak points to hackers), this error won't show up.

It's very likely you have a difference in output, perhaps using a stripped down template for local development in comparison to a full blown production template on site. Or anything else making a difference on the matter of any HTML output (even just a space) before that header is sent.

And you'd likely see that error in error logs, if reporting only suppresses output (display_errors:Off) but not logging of errors (log_errors: On), as it should in production. Look at phpinfo(); to see where your error_log file is and see if your hoster gives you access to it via FTP.

Bye, Olaf.
1
AdamTrying to learn phpAuthor Commented:
Thanks Olaf, Dave.

Olaf - I turned on the error settings and the warnings which were displayed suggested (to me) what Dave and you were suggesting:


Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/orango5/public_html/mini_profiles_TEST.php:1) in /home/orango5/public_html/private/initialize.php on line 6

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/orango5/public_html/mini_profiles_TEST.php:1) in /home/orango5/public_html/private/initialize.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at /home/orango5/public_html/mini_profiles_TEST.php:1) in /home/orango5/public_html/mini_profiles_TEST.php on line 86

I then tried a number of things to get this to work - taking guidance from this Stack Overflow Page.

1. Using header function with exit(), and also removing any html from my page so it was all php  - didn't work.
2. Using header function with ob_start() and ob_end_flush() and removing html  - didn't work.

In the end what worked was just using this code instead of using the header()
echo "<script>location.href='no_results.php';</script>";

Open in new window


The only slightly annoying thing is that there is a bit of a delay between the re-direct occurring. After I press the submit  button on my search form it goes to the 'typical' page for a split direction before being redirected. Not the end of the world I suppose, and something I''m willing to put up with to make some progress.

Thanks Olaf and Dave for your guidance. It's always a head in my hands moment when something which worked great on my development server fails on the live server but I guess that's part of web development.

Bye.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

AdamTrying to learn phpAuthor Commented:
Thanks Dave and Olaf.  The use of the javascript seemed to work for me in the end, but you both steered me in the correct direction. Cheers.
0
AdamTrying to learn phpAuthor Commented:
Hi Olaf - just to clarify also -
It's very likely you have a difference in output, perhaps using a stripped down template for local development in comparison to a full blown production template on site. Or anything else making a difference on the matter of any HTML output (even just a space) before that header is sent.

As I understand it, I am using the same files locally as I am on my live site. I have just created them, tested locally and then uploaded the same files to the live server via ftp.

Thanks
0
Olaf DoschkeSoftware DeveloperCommented:
Well, there must be some difference why you don't get an error reported locally, we may never find out why exactly.

That aside buffering all output also could work, starting to buffer just must be done as the first thing. Notice, this isn't about the HTML <header> section, this is about HTTP protocol headers. And once you have that buffering the moment you instead want to send headers you have to flush the buffers without sending them, or you get the exact same problem, just delayed.

output started at /home/orango5/public_html/mini_profiles_TEST.php:1
Well, what do you have in line on of that mini_profiles_TEST.php? Is there something before a <?php starting tag? That's your output, then. Even if it's just space, tab or the byte order mark bytes notepad puts in front, when you save as UTF-8.

Using javascript means you send out an HTML page that does the redirect client side. With today's typical settings of browsers, this should work almost anywhere, but it would be ideal to solve this on the PHP side.

Bye, Olaf.
0
AdamTrying to learn phpAuthor Commented:
Hi Olaf,

One difference is that the php version on my local server is different (older than) from that on my live server. The local version is 5.6 and the server has 7.1. I have been meaning to update the local version to match.

I have noticed I use the header redirection function quite a bit, sometimes passing a value along with the redirect, so I will try again with the buffering solution and update this thread.

Thanks

Adam
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.