0

header() will not redirect to the page I wanted. There are no error msgs and header function works fine in Xampp but not in production web-server.

What I'm trying to accomplish is that in the table of links, when I click on APR button, the STATUS column changes to "Approved" immediately. This work in localhost environment but not in production server.

I've already tried ob_start and ob_end_flush, I've also checked if there are any white spaces or lines.

I also removed "./" preceding index.php.

// this is the reservation.php
if (isset($_GET['apr'])) {
  $rsID = $_GET['apr'];
  $query = $conn->query("UPDATE reservations SET reservStatus = True WHERE 
  reservID = $rsID");
  header("location: ./index.php");
}

// this is the index.php
<?php
   require 'header.php';

   if (isset($_SESSION['user_info'])) {
     include 'reservation.php';
   } else {
     include 'login.form.php';
   }

   include 'footer.php';

I expected it would redirect to the index.php and from there, if session has been started, it would direct the page back to reservation.php

Keith
  • 31
  • 6
  • 3
    Protip: ***never*** use `GET` requests to *change* something. `GET` is for getting information, `POST` is for working with it. – Niet the Dark Absol Apr 19 '19 at 17:00
  • Did you look for `headers already sent` error messages on your log? – RiggsFolly Apr 19 '19 at 17:00
  • What do you do in the code above the little piece of `reservation.php` that you show us – RiggsFolly Apr 19 '19 at 17:02
  • 1
    Not your current issue BUT you are open to SQL injections. Parameterize that query. – user3783243 Apr 19 '19 at 17:02
  • Did you check the Network tab in the F12 tools for your browser to see what response was sent by the production server? – Dave S Apr 19 '19 at 17:02
  • I'm a newbie to php coding.. How do I check what response was sent by the production server? – Keith Apr 19 '19 at 17:09
  • To the question "What do you do in the code above the little piece of reservation.php that you show us?" basically bunch of same redirection using same header function. – Keith Apr 19 '19 at 17:15
  • Thanks for the tip, Niet the Dark Absol :) – Keith Apr 19 '19 at 17:20
  • Can, you provide the link of your website – vaku Apr 19 '19 at 17:22
  • did you try to [show all errors and warning](https://stackoverflow.com/a/5438125/8071073) in php – vaku Apr 19 '19 at 17:45
  • vaibhav kumar: do you mean add additional codes to my codes? As I mentioned, I'm a new to the php coding.. Sorry if I don't understand what you mean.. I don't know where to add the show all errors and warnings and if the error and warnings will show on the browser or do I need to do something to check errors – Keith Apr 19 '19 at 17:51
  • try adding ```error_reporting(E_ALL);``` ```ini_set('display_errors', 1);``` lines at the top of PHP page – vaku Apr 19 '19 at 17:53
  • yes, errors and warning will be displayed on browser, brother... – vaku Apr 19 '19 at 17:59
  • Warning: Cannot modify header information - headers already sent by (output started at /volume1/web/cdnj/header.php:6) in /volume1/web/cdnj/reservation.php on line 121.. Getting somewhere! Thanks – Keith Apr 19 '19 at 18:02
  • 1
    Search "PHP cannot modify header" here and maybe with Google to find a list of things to check. To see what is sent by PHP as a response, learn to use the F12 key developer tools view in Google Chrome browser. The Network tab, click the preserve log checkbox. The network log with show the request from browser and the response from PHP – Dave S Apr 19 '19 at 18:06
  • Thanks Dave S, you've been very helpful! – Keith Apr 19 '19 at 18:13
  • vaku: Line 121 is "header("location: ./index.php");" statement. – Keith Apr 19 '19 at 18:17
  • Thanx @keith see my answer below, if that works, remove the the lines that shows the error and warning, they may lead to leak sensitive information of your server... – vaku Apr 19 '19 at 18:20
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – miken32 Apr 20 '19 at 05:07

1 Answers1

2

The PHP script will not stop even after redirect header, You have to stop the PHP script from keep loading by calling die(); function

// this is the reservation.php
if (isset($_GET['apr'])) {
  $rsID = $_GET['apr'];
  $query = $conn->query("UPDATE reservations SET reservStatus = True WHERE 
  reservID = $rsID");
  header("location: ./index.php")

  // Add these two lines
  die();
  exit();
}

You too can add these line below every redirect header in your script, Thanks....

update

I think I am getting your problem, what I have get till now is that you are redirecting the page after your welcome message in header section of your website is displayed,

So actually you can't call header function after any type of output(via PHP) send to the browser (In your case the welcome message in header section) because PHP initially send Content-type header to the browser before you first display any thing on browser from PHP

Now, you have two ways to that

1) Include JavaScript for redirection

replace your header line in reservation.php with this

echo ("<script> window.location = 'http://cdnj-nas.synology.me/cdnj/index.php'; </script>");

Or, if this not works

echo ("<script> location.replace('http://cdnj-nas.synology.me/cdnj/index.php');  </script>");

2) you can check this condition of redirection in header.php file at top

vaku
  • 697
  • 8
  • 17
  • I added those two lines as you have instructed, no changes. I've added show all errors and warning and I've got a warning msg: Warning: Cannot modify header information - headers already sent by (output started at /volume1/web/cdnj/header.php:6) in /volume1/web/cdnj/reservation.php on line 122 – Keith Apr 19 '19 at 18:25
  • what is on line 6 in header.php – vaku Apr 19 '19 at 18:25
  • Line 6: how can I fix this? I need in line 6, don't I? – Keith Apr 19 '19 at 18:36
  • Is there any redirection made in header.php file, as you did in reservation.php file?? – vaku Apr 19 '19 at 18:38
  • No, There are no redirections in header.php. It's just checking if user is logged-in or not, if the user is logged-in, welcome messages. – Keith Apr 19 '19 at 18:47
  • I tried both lines of JavaScript but i'm getting a blank page beneath the welcome message. I really appreciate your help with this. However this turns out, I love what you're trying to do for me.. I'm indebted to you! – Keith Apr 19 '19 at 19:38
  • Thanks @Keith can you please view the console and tell me if their is any error, you can go in console via ctrl+shift+I in browser window and the navigate to console tab , and too reload the page after that – vaku Apr 19 '19 at 19:46
  • And Include the show all error and warning code at the top of your header.php file – vaku Apr 19 '19 at 19:51
  • I'm getting following error message: http://cdnj-nas.synology.me/favicon.ico 404 (Not Found) favicon.ico:1 – Keith Apr 19 '19 at 19:55
  • Did you add a semicolon after echo statement? If yes did you try the second option? – vaku Apr 19 '19 at 19:57
  • Yes, I tried the second option too. I'm getting the same error msg @login page before logging-in when refreshed. echo ("") <---this is the exact code i added. – Keith Apr 19 '19 at 20:04
  • i copied your code pasted to my codes.. I'm not sure semicolon is missing from where. As I said, following is the exact copy of your code--> echo ("") – Keith Apr 20 '19 at 22:26
  • Sorry my mistake, See now a semicolon after ```echo ("")``` ```;``` <- this one ... – vaku Apr 21 '19 at 05:27
  • Sorry for the late reply. That seems to have solved my problem with the header() problem! Thank you so much for helping me with this. One minor additional question: My monitor seem to flicker the moment in click on the link. Any ways to address this minor problem? – Keith Apr 25 '19 at 14:36
  • Hello @Keith, glad, you get some help form my side...can you please share the link about the new question...Thanks – vaku Apr 25 '19 at 17:02
  • I shared your answer on my Facebook acct..! Thank you again for all your help!!! – Keith Apr 25 '19 at 17:31
  • It's my pleasure...Thank You...^_^ – vaku Apr 25 '19 at 19:06