Bobcares

PHP header not redirecting- How we fix it?

by | Feb 9, 2021

Wondering how to fix PHP header not redirecting issue? We can help you.

This redirection error occurs due to some incorrect output buffering settings and wrong header formats.

At Bobcares, we often receive requests to fix redirection errors in PHP as part of our Server Management Services.

Today, let’s discuss this error in detail and see how our s Support Engineers fix it effectively.

Steps to fix PHP header not redirecting

Before going into the steps for fiing this issue let’s see some uses of PHP header?

The header() function in PHP sends a raw HTTP header. This raw data contains the request made by the server as information.

And, we call header() before any actual output is sent. It gives a clear idea about the object sent in the message body.

Also, the Location: parameter of the header returns a redirect status code to the browser.

Usually, we can redirect it either to a URL or some file.

In PHP, redirection to a URL will be of the following format:

< ?php header("Location: http://www.redirect.to.url.com/"); ?>

Similarly, to redirect to a file, we use the following command:

<?php header("Location: anotherDirectory/anotherFile.php"); ?>

Often this redirection results in some tricky errors, we will see how our Support Engineers fix them.

Fixing PHP header not redirecting

PHP redirections are usually set when we get failed sessions. We can check this with the code given below:

if(
!isset($_SESSION['SESS_MEMBER_ID'])
|| (trim($_SESSION['SESS_MEMBER_ID']) == '')
AND !isset($fb_login)
) {
header("location:login.php?msg=Please+Login+Again.");
}

For failed session logins, we will try redirection to another page and this is when we notice that redirection has failed and a black page showed up.

This generally happens due to an error with output buffering.

If we use header(“Location: “);, then it is necessary to use ob_start(); earlier in the script.

ob_start() function will turn the output buffering ON. While output buffering is active no output is sent from the script other than headers until we call ob_end();

Calling header(“Location: “); after content has already been output to the browser can cause problems.

So, we ensure that no output is sent before calling the header.

Setting ob_start() at the beginning of the PHP file heps to resolve the error.

Similarly, errors in the header format can also cause redirection failures.

It is important to make the “Location” start with uppercase. Also, the header contents must be inside double-quotes.

[Still facing PHP redirection errors?- We’ll help you.]

Conclusion

In short, the PHP header not redirecting error occurs mainly due to the absence of ob_start() function, incorrect header formats, and so on. Today we discussed this error in detail and saw how our Support Engineers fix this for our customers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

9 Comments

  1. Renuka A

    Fixwd My Probllem

    Reply
  2. Bernd

    Thank you for this helpful article, which led me to the solution after hours of searching for the error. Although the program worked fine on my local xampp-server, I had a white-screen (blank-screen) on the internetserver.
    Using ob_start() and ob_end() helped.

    Reply
    • Hiba Razak

      Hi Bernd,
      Thanks for the feedback.We are glad to know that our article helps you solves the issue.

      Reply
  3. KC Sharma

    Fixed my problem by this solution . Thankyou!

    Reply
    • Hiba Razak

      Hi,
      Thanks for the feedback.We are glad to know that our article helps you solves the issue.

      Reply
  4. Christian

    Thank you very much !!
    ob_start() was the solution.

    Best regards

    Reply
    • Hiba Razak

      Hi,
      Thanks for the feedback. We are glad to know that our article was helpful for you 🙂 .

      Reply
  5. keiner

    thanks for your post. it works for me.

    Reply
    • Hiba Razak

      Hi keiner,
      Thanks for the feedback. We are glad to know that our article was helpful for you 🙂 .

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF