PHP header error [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have an issue with my PHP web application that involves the use of headers throughout my program files. To be more specific, I created a file called "error.php" that looks like this:
<p><?php
$errorCode= $_GET['message'];
switch($errorCode)
{
case "1":
echo"<br>Sorry, all input fileds except for apartment number must be completed.<br>";
break;
case "2":
echo "<br>You have entered an invalid name format.<br>";
break;
case "3":
echo "<br>Name update not successful.<br>";
break;
case "4":
echo "<br>Phone number update not successful.<br>";
break;
case "5":
echo "<br>You have entered an invalid phone number format.<br>";
break;
case "6":
echo "<br>You have entered an invalid street addres format.<br>";
break;
case "7":
echo "<br>Apartment number update not successful.<br>";
break;
case "8":
echo "<br>City name update not successful.<br>";
break;
case "9":
echo "<br>State name update not successful.<br>";
break;
case "10":
echo "<br>Zip code update not successful.<br>";
break;
case "11":
echo "<br>Email address update not successful.<br>";
break;
case "12":
echo "<br>You have entered an invalid email address format.<br>";
break;
case "13":
echo "<br>You have entered an invalid password format.<br>";
break;
case "14":
echo "<br>Can not find what to update.<br>";
break;
case "15":
echo "<br>You have entered an invalid apartment number.<br>";
break;
case "16":
echo "<br>You have entered an invalid password format.<br>";
break;
case "17":
echo "<br>You have entered an invalid email address format.<br>";
break;
case "18":
echo "<br>User not found in the database.<br>";
break;
case "19":
echo "<br>User ID not found in database. Login Failure.<br>";
break;
case "20":
echo "<br>Failed connection to the database.<br>";
break;
case "21":
echo "<br>Street Address update not successful.<br>";
break;
case "22":
echo "<br>User already exists.<br>";
break;
case "23":
echo "<br>Failed to create a new user account.<br>";
break;
case "24":
echo "<br>You have entered an invalid address format.<br>";
break;
default:
echo "<br>You have an unknown error.<br>";
}
?> </p>
THE PROBLEM is that somewhere else in the application I would use statements like the following:
header("location:/view/error.php?message=23");
header("location:/view/error.php?message=21"); and so on...
I now come to realize this is problematic because I constantly get Warnings like,
"Warning: Cannot modify header information - headers already sent by (output started at /home/content/47/12002447/html/model/userRegistrationAction.php:6)/home/content/47/12002447/html/model/userRegistrationAction.php on line 92"
And I get plenty of them mostly pointing to the same file. My QUESTION is, is there a way that I can direct pages to "error.php" in a way similar to the way I am currently using but with using header?
I do not want to use "include("error.php?message= 'some #')" because it introduces a ton of overhead.
I would like to thank you guys in advance for any help

make sure you call the 'header' function BEFORE any output. In other words, no HTML or javascript can be called earlier than the 'header' function or it will fail with the error message you describe. as a good rule, put all 'header' function calls above the 'html' open tag in your files

Related

JSON encode gives "malformed json" error on a file that is completely valid JSON

So, i'm creating php page that sends JSON data to other app. I got the code working, but then it mysteriosly stopped working. I'm getting malformed JSON error, even though JSON validator says my file is fine. When i try other JSON file it works fine.
Here's the JSON:
[{
"value1": "Example",
"value2": "Other example"
}]
And here's the PHP (don't mind the Finnish variable names):
$url = "velat.json";
$velat_json = file_get_contents($url);
$velatarray = array();
$velat = json_decode($velat_json);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
foreach($velat as $velka){
$velatarray[] = $velka;
}
header("Access-Control-Allow-Origin: *"); //CORS
header("Content-type: application/json");
print json_encode($velatarray, JSON_PRETTY_PRINT);
What's really weird is that it USED to work. I added feature that allowed me to add new items to JSON file via angular app and even that worked. Then i deleted the added item manually and tried encoding it in utf-8 and suddenly it stopped working, even after i undid all changes and reversed it back to bare bones. How is this happening?
Edit: fixed, BOM was causing the issue

Steam API get current user status

I'm trying to use the steam web API to show the current status of ONE steam user if he is (online,away,offline,looking to trade) etc and have setup a PHP switch as follows:
<?
$status = $steamprofile['personastate'];
switch($status) {
case 0:
echo "offline!";
break;
case 1:
echo "online!";
break;
case 2:
echo "Busy.";
break;
case 3:
echo "Away.";
break;
case 4:
echo "Snooze.";
break;
case 5:
echo "Looking to trade.";
break;
case 6:
echo "Looking to play.";
break;
default:
echo "Unknown status";
}
echo $status;
?>
It works great but only shows the user status when logged unto the site and not simply when the user is online/away/offline etc on the steam platform.
I'm wondering if anyone know how to fix so it displays the current user status on steam, not just if he/she is logged in on the website.

How to get user input for switch statement in php?

As questioned above I'm trying to get user input in my PHP script to do further tasks. Is there any method in PHP to get user input like 'scanf()' used in C
<?php
echo "Swapping Numbers </br> Please select the method: </br>";
echo "1. Using 3rd variable </br> 2. Using add/sub </br> 3. Using mul/div";
//read user input $choice
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
the STDIN constant is pre-defined and available for you to use immediately.
You can get user input using fgets and fscanf
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
For example
<?php
echo "Enter a number: ";
fscanf(STDIN, "%d\n", $number);
switch ($number) {
case 1: echo "one\n"; break;
case 2: echo "two\n"; break;
case 3: echo "three\n"; break;
default: echo "I can't count that high\n";
}
Check out the I/O docs for more detailed information
You should use readline() :
var_dump(readline('Enter number: '));
You can handle user input from form element or from url.
in your case you can use like this.
$choice = $_GET['choise'];
User will type like this
http://localhost/your_file_name.php?choise=option
than you should use your switch
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>

Get the data from json url using php?

This is a json api : https://jobs.github.com/positions.json?description=java&page=1
I want get the data from this url.
<?php
$url = file_get_contents('https://jobs.github.com/positions.json?description=java&page=1');
var_dump(json_decode($url,true)); ?>
this code return null
I also check the url in json validator: http://jsonformatter.curiousconcept.com/ the json is valid but i con't able get the data from this url please help me...
Try this script to determine what the problem is. If there is no JSON module installed (see #julian comment), you can try to use PHP implementations of JSON like this: http://pear.php.net/pepr/pepr-proposal-show.php?id=198
if (! extension_loaded('json')) {
echo 'Module JSON not available!';
exit();
}
$url = file_get_contents('https://jobs.github.com/positions.json?description=java&page=1');
$data = json_decode($url,true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}

Unit testing of json encoding/decoding against errors

So basically I have the code which is giving me the message from json_last_error():
$msg = 'Unknown error';
switch (json_last_error()) {
case JSON_ERROR_NONE:
$msg = null;
break;
case JSON_ERROR_DEPTH:
$msg = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$msg = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$msg = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$msg = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
return $msg;
For testing purposes I want to raise all the errors from this list to have 100% coverage, but I can't raise JSON_ERROR_STATE_MISMATCH.
Can anyone help giving me the example with either encoding or decoding, with any parameters, which can produce this error?
$j = '{"j": 1 ] }';
json_decode($j);
var_dump(json_last_error() === JSON_ERROR_STATE_MISMATCH); // true
How I found it: just checked the source code :-)

Categories