PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] How return http_code 401 with PHP

 May 17, 2022     curl, http-headers, php     No comments   

Issue

I'm using cUrl in the client side.

On my webservice (ws.php), i've this simple code :

<?php
header('HTTP/1.1 401 Unauthorized', true, 401);
echo 'This is an error';
exit;
?>

When i call this ws.php page, i still have http_code = 200.

Here is my cUrl code is client side :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "webservice_url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables); //array()

$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);

$code = 200 even i put the header code "401".


Solution

try this on your ws.php:

<?php
ob_start();
header('HTTP/1.0 401 Unauthorized');
echo 'This is an error';
exit;
?>

Let me know if this works or not ?



Answered By - Kush
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing