I created a function to allow me to debug PHP scripts so long as a variable ($debug) is set to 1:
function debug($msg) {
if ($debug == 1) {
echo $msg;
} else {
return false;
}
}
That way, at the top of my script (before the functions.php file is called), I write:
$debug = 1;
to enable debugging, then:
debug("Function executed: " . $data);
at certain points so that I know string values/whatever at that point, with the desired response being the message displayed upon the screen.
However, regardless of what the value of the $debug string is, I never see any echo'd statements.
How can this be achieved?
Debug is not available to your function because it is out of scope. You either:
Need to pass it as a parameter
Use the global keyword to include it in your function (discouraged)
.
function debug($msg, $debug){
if($debug==1){
echo $msg;
} else {
return false;
}
}
or
function debug($msg){
global debug;
if($debug==1){
echo $msg;
} else {
return false;
}
}
It's difficult to say because you provided too few data.
The reason can be that your $debug variable is not known inside a function. Because using globals is not adviced, consider using constants define("DEBUG",1);.
EDIT
I presented within another question how I use a class for doing the same thing as class names are also globally accessed.
The global variable is not accessible to functions until you make it so. Eg:
function debug($msg( {
global $debug;
etc...
PS: Please, don't do this. Find a better way. Really.
$debug is your global variable, so it is not accessible in your function
There is also the possibility to declare a const, (and then just insure the namespace is correct), like this:
const debug = true;
function newPrint($msg) {
if (\debug === true) {
echo $msg;
} else {
return false;
}
}
\newPrint("heey");//will print "heey"
so just dont use a variable, but use a const.
Related
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've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.
This code use protect function . to do permission access
I look at address bar found it still in protect page
this is "protect page":
foreach($access_level as $k => $v)
{
// print_r($v); // output 12
protect($v);// call function in loop to get the values of array
}
}
global $v ;
function protect($v){
if($_SESSION['sessionloginid']==true )
{
if( $v ==1)
{header(" location: http://localhost/database/agtdatabase/agt_site/display/display.php");}
}
}
#Mark B above has it right.
Also - Headers are only able to be set if there is no output to the browser when they are run - If you print_r($v), headers are already sent out. Make sure your call to your function is the top possible line, right after session_start().
<?php
session_start();
protect();
/// Other code ///
function protect() {
if($_SESSION['sessionloginid']!==true) { header("Location: http://someplace/index.php"); }
}
Use of header("HTTP/1.1 403 Unauthorized" ); may be a good idea instead of redirecting, if you don't expect a user to see the message unless they are poking around where they shouldn't.
You may also be able to use header("Location: http://someplace/",TRUE,403); to send a 403 code and a redirect at the same time (so any APIs you may use against this site will recognize if they failed to log in correctly).
You're passing $v as an argument to your function, but the function definition has no arguments:
function protect(){
^---no args
PHP has exactly TWO variable scopes: local, and global. The $v you're making global inside the function is probably NOT going to see the $v you defined in the foreach loop above. e.g.
$v = 1; // global scope
function foo() {
$v = 2; // local scope
bar();
}
function bar();
global $v;
echo $v; // outputs 1
}
You should have
function protect($v) {
if ($v == .....) { ... }
}
instead.
I am new to CodeIgniter. I was just thinking, is there any way I can send any variable to the constructor of a controller, the same way I can do in Java when I create an object?
You can send variables to controller function through URL.
For example, if your URL is www.domain.com/index.php/reports/userdata/35
then your controller function in file controllers/reports.php would look like:
function userdata($userId) {
.....
}
I don't know why you want do this and where you intend to get the variable you are sending from but this does work in this case:
function __construct($f=null) {
parent::__construct();
if($f){
return $f; //Here use the variable for whatsoever you want.
}
}
function testvariable($id) { //Using $id, you could still get the value from url
$myVariable = 3; //Or you could just hard code the value
if($id){
$myVariable = $id;
}
echo $this->__construct($myVariable);
exit;
}
When you run http://localhost/controller/testvariable/54
You'd get the result 54
When you run http://localhost/controller/testvariable
You'd get the result 3
Outside these, the other option would be to define the variable in the construct.
I use global variable $table_prefix to differ whether I work on Word Press or WPMU. I need this global variable for my plugin. But is there any better way to check whether your pluggin is working on Word Press or WPMU?
In WPMU a global variable named wpmu_version should be set.
for my checks I use a function I found
// from http://frumph.net/wordpress/wordpress-plugin-theme-check-for-multisitewpmu/
// check for multisite. Returns boolean
function check_this_is_multsite() {
global $wpmu_version;
if (function_exists('is_multisite')){
if (is_multisite()) {
return true;
}
if (!empty($wpmu_version)){
return true;
}
}
return false;
}
use it like this
if(check_this_is_multsite()){
// is on wpmu
} else {
// is on single
}
You could define a constant:
e.g.
define('ENVIRONMENT', 'WP');
define('ENVIRONMENT', 'WPMU');