0

I want to set http response code according to condition in php page.

if($Variable== true)
{
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);           
    include('404.php');
    die();
}

I am looking something like below.

 if($Variable == true)
      // http response code =404
   else
     // http response code =200

Is it possible to do this thing in php?

I have tried as below to solve it. But not worked.

if($Variable ==true)
{
    http_response_code(404);
    include('404.php');
   die();
}
Bhavesh Patel
  • 115
  • 1
  • 2
  • 12

2 Answers2

1

You can use php http_response_code() which Get or Set the HTTP response code.

<?php

if($Variable == true)
      // http response code =404
    http_response_code(404));
   else
    http_response_code(200))
     // http response code =200
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
-1

Try this. it might help you.

   <?php
   if($Variable == true)
     header("Status: 404 Not Found");
   else
     header("HTTP/1.1 404 Not Found");
sree
  • 389
  • 5
  • 17