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.
Related
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);
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 have a function that I need to timeout and output an error message.
I have found the set_time_limit() function, but I dont think I am using it right.
I have tried...
... some code ...
set_time_limit(12);
$client->sendHttp(URL, TIMEOUT_CONNECT, TIMEOUT_READ);
if (set_time_limit(12) != true){
$_SESSION['Message'] = "Transaction Timed Out!";
}
... some code ...
That's the best I could come up with but it doesn't work. Can you suggest anything?
set_time_limit limits the scripts time, the script all together will end after that amount of time no code will be executed after that
$client->sendHttp should return false, null if a timeout has been reached, read the documentation on that function to see what it will actually return.
Normally if the script timeout, the web server stops it and return an error while You have only a little chance of handling it by Yourself - by defining shutdown function.
But You could use a simple function of Your own, like this one:
function check_timeout($start) {
if(microtime() <= $start + MAX_EXECUTION_TIME)
return true;
return false;
}
while the MAX_EXECUTION_TIME constant would be defined somewhere like
define('MAX_EXECUTION_TIME', 10000); // 10 seconds
Now somewhere in Your code You could do:
// some code...
$start = microtime();
foreach($array as $key => $value) {
if(check_timeout($start)) {
// do something
} else {
// set HTTP header, throw exception, etc.
// return false; // die; // exit;
}
}
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 am building a captcha class. I need to store the generated code in a PHP session. This is my code so far:
<?php
class captcha
{
private $rndStr;
private $length;
function generateCode($length = 5)
{
$this->length = $length;
$this->rndStr = md5(time() . rand(1, 1000));
$this->rndStr = substr($rndStr, 0, $this->length);
if(session_id() != '')
{
return "session active";
} else {
return "no session active";
}
}
}
?>
And using this code to check:
<?php
include('captcha.class.php');
session_start();
$obj = new captcha();
echo $obj->generateCode();
?>
But it doesn't output anything to the page, not even a PHP error. Does someone know why this is? And is there a better way I can check if I've started a session using session_start()?
Thanks.
$this->rndStr = substr($rndStr, 0, $this->length);
return $rndStr; //You return before the if statement is processed
if(session_id() != '')
{
return "session active";
} else {
return "no session active";
}
Answer in commented code above
Edit: And you changed your question and removed the return line, not nice for people bothering to answer :)
i was testing your class, seems ok here, got session active on page, maybe you want to try this line :
include(dirname(__FILE__).'/captcha.class.php');
You do have an error, so I suggest checking your error reporting level and display errors setting - lots of short tutorials on how to do that.
Notice: Undefined variable: rndStr in
/home/eric/localhost/test.php on line
12
Of course this is because you wrote $rndStr instead of $this->rndStr.
When I ran your code, aside from the error, I saw the expected output.
session active
Are you able to successfully output to the browser in other scripts?