I am a novice and I work in PHP.
My English is not very good. If you see a typo, please edit it.
I need a function that changes the status of the site. Like the following function:
var_dump(http_response_code()); // return 200
function changeStatus($from, $to) {
// The code I need
}
changeStatus(200, 404);
var_dump(http_response_code()); // return 404
Is such a thing possible at all?
please guide me
This code will solve your problem
function changeStatus($response_code) {
// The code I need
http_response_code($response_code);
}
changeStatus(404);
var_dump(http_response_code());
This is wrong because it returns the previous result
var_dump(http_response_code(404)); // return 200
Try this. This answer is safer
function changeStatus($responseCode)
{
if (http_response_code($responseCode))
return true;
else
return false;
}
changeStatus(404);
Related
I'm trying to make things more simple in my Laravel application. Therefore I'm wondering how's it possible to NOT redirect the Client if one of my function returns true and how to redirect if it is a string (not true).
So let's say I have a function like this:
public function returnText() {
$r = rand(1,100);
if ($r > 50)
{
$redir = true;
}
else
{
$redir = 'http://google.com';
}
return $redir;
}
What I want within my Laravel's controller's function is to redirect the user if the result is an URL and don't redirect if it's simply 'true' (or anything basically it doesn't matter if I return true or something else).
Now I can obviously do that with a few lines but I'd like to optimize it only to take 1 max 2 lines.
public function redi() {
return redirect(CustomFunction::returnText());
}
I've tried this approach but it obviously throws some error, because it can't understand 'true'.
Any ideas on how to achieve this with the least lines of code?
P.S.: using Laravel 5.5
redirect() function requires uri to redirect to and true boolean value is invalid, hence the error.
So, you can do something like this in your function:
public function redi() {
$redirectUrl = CustomFunction::returnText();
if (!is_bool($redirectUrl)) {
return redirect($redirectUrl);
}
return;
}
i.e. only redirect if it gives you anything but a boolean value.
The Overview
I've been experimenting some features which I've learn't using PHP, last night I was working on anonymous functions and for some strange reason when I var_dumped the function it kept returning null.
The Code
Below is the code I've written.
The findOrFail function,
public static function findOrFail($iD, $successCallback = null, $failCallback = null)
{
$db = new Database();
$db->select("users")->fields(["*"])->where(["id" => $iD])->execute("select");
if ($db->rowCount() == 1) {
if (is_callable($successCallback)) {
return $successCallback();
} else {
return true;
}
} else {
if (is_callable($failCallback)) {
return $failCallback($iD);
} else {
return false;
}
}
}
In test.php,
require_once "config.php";
var_dump(User::findOrFail(1, function () {
echo "Found.";
}, function ($iD) {
echo "Failed.";
}));
The Output
The ID 1 exsits so I expect the see when dumping string and the contents to be "Found." however I see this:
Found.NULL
What I have tried?
I looked at another question related to this issue and it said
that it was because of a buggy PHP version (5.3?). So I checked my
PHP version and it is 5.5.8.
I thought maybe because the default parameters ($successCallback and $failCallback) are set to equal null that that may be causing the error to occur. However some quick changes to the code (to remove the null) showed that it didn't fix anything.
So my question is, Why is it showing null? If anyone could shed some light on this issue it would be much appreciated.
Your anonymous functions don't return anything, they just call echo to print something. Use:
return "Found";
and
return "Failed";
I am trying to implement error proofing to a log in script. But, I cannot get it to work? I have no idea what is going on, or why it doesn't work like I expect it to. I have tried everything, please advise.
This is the method I am calling:
public function i_exist($this_username)
{
//$host_array = null;
//$host_array = $this->collection->findOne(array("Username" => $this_username));
//if ($host_array['Username'] = $this_username)
//{
return true;
//}
//return false;
}
This is how I am calling it:
if (!empty($_POST['Username']))
{
$host = new Host();
$event = new Event();
if ($host->i_exist($_POST['Username']))
{
header("Location: http://www.drink-social.com/error.php?login=duplicate");
}
It is supposed to check the database and see if that username is already in use. But it never directs to the error page? I have even tried commenting everything out and returning true, and returning 1. Nothing?
Any advice?
When you call header(); you will also need to call exit(); otherwise the script continues running.
I'm trying to detect whether an image exists on a remote server. However, I've tried several methods and can't get any of them to work.
Right now I'm trying to use this:
if (!CheckImageExists("http://img2.netcarshow.com/ABT-Audi_R8_2008_1024x768_wallpaper_01.jpg")) {
print_r("DOES NOT EXIST");
} else {
print_r("DOES EXIST");
};
function CheckImageExists($imgUrl) {
if (fopen($imgUrl, "r")) {
return true;
} else {
return false;
};
};
But it returns 'true' whether the image actually exists or not (the above image should, but change it to gibberish and it still will return 'true'). I have a feeling it could be because if the URL does not exist, it redirects to the homepage of the site. But I don't know how to detect that.
Thanks for any help!
Use cURL.
After fetching the resource, you can get the error code calling curl_errno().
The chances are you are getting a HTML page back into your $imgUrl that contains "404 image not found" or something similar.
You should be able to check the response for a code indicating that the request failed or redirected.
This should do the trick (using image size):
if (!CheckImageExists("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png")) {
echo 'DOES NOT EXIST';
} else {
echo 'DOES EXIST';
};
function CheckImageExists($imgUrl) {
if (#GetImageSize($imgUrl)) {
return true;
} else {
return false;
};
};
Got it working with Seb's method. Just used YQL to inspect the actual content of the page and determine if it's an error or not.
Here is the code:
ob_start(array(&$dispatcher, 'outputCallback'));
include($file);
ob_end_flush();
function outputCallback($string) {
if(ob_get_level() == 1) {
$static =& ParserStatic::getInstance();
return $static->insertToppings($string);
}
return false;
}
The problem is when I return $string it behaves OK, but when it executes
the object assignment, it gives a blank screen. What's going wrong?
Have you tried checking your web server's error log to see if PHP is throwing an error? That should help you identify the cause of the problem.