I want to have a config file that basically says something like (Account: on/off), where an admin can choose on or off. And then, in my main script, i want a bunch of if else statements that says if its on, do this, if off, do this.
Any suggestions?
config.php:
<?php
$account = 'off';
main_script.php:
<?php
include('config.php');
if ($account == 'on') {
//do this
} else {
//do something else
}
Config files usually define global constants available everywhere in your code and often seen in the variable $GLOBALS['config']. Config files are normal PHP files that get included using include() or better include_once() at the very top of your applications main file.
include_once('config.php');
if ($GLOBALS['config']['admin']) doThis();
else doThat();
http://php.net/manual/de/reserved.variables.globals.php
Related
I have been looking for a way to run one piece of code only once in php. I want to create and set a variable at the beginning of the webpage and whenever I call that file, the code will ignore that line.
I found this in which there are basically two suggestions: setting a session variable and creating a lock file which I don't want to since there might be many users at the same time and I need to have a lock file for each one of them.
I tried the first option but it doesn't work.
Here are my two files:
config.php
<?php
if (at the beginning){
$variable = something
echo 'hello';
}
else
do something
?>
second.php
<?php
require_once 'config.php'
//do some other stuff
?>
Whenever second.php is called, it prints 'hello' in the webpage, so if (at the beginning) returns true even though it is not at the beginning. Note that, I check whether at the beginning by if(!isset($_SESSION['done'])){ $_SESSION['done'] = 'done'; }
I wonder what is my mistake and how can I solve the problem. Any help is appreciated.
The program flow for config.php should be:
<?php
session_start();
if (!isset($_SESSION['done'])) {
$_SESSION['done'] = 1;
echo 'hello';
}
else {
// do something
}
?>
Hypothetical scenario, I have these files:
file1.php:
#!/bin/php
<?php
echo "starting\n";
$pid = pcntl_fork();
define('ME', 'Parent');
if($pid == 0) {
include 'file2.php';
exit;
}
echo "I am: ".ME;
file2.php:
#!/bin/php
<?php
define('ME', 'File2');
echo "Defined: ".ME;
When I run file1.php, I get an error, because ME is already defined and you can't re-define a constant. How can I tell php to reset ALL defined constants and variables, so that it can include a new file without carrying anything over? As far as I can see, my only options is to use shell_exec or similar to run a shell command to start a new php process, but that seems really roundabout - is there any way to clear out existing defined constants?
My specific use case is a large framework with many files included at various stages, much of which I can't touch, so you can assume that the only thing I'm able to modify in the above example is the contents of the if($pid == 0) { ... } brackets.
I'm fairly new to PHP and am having trouble with variables. When I put everything in one PHP file, it works but my question is should this work? I am under the impression that when you include a file then the variables are also included. Assuming that is true, when connecting to a DB, is it good practice to connect in a separate PHP file then include that into pages where you need to use the DB?
page1.php
<?php
$test = "true";
?>
page2.php
<?php
$test = "false";
?>
home.php
<?php
include 'page1.php';
include 'page2.php';
echo $test;
?>
Expected output false but I am getting true.
When you include a file, PHP compiler extends the code with code in included file.
Basically the code:
include 'page1.php';
include 'page2.php';
echo $test;
changes to:
$test = "true";
$test = "false";
echo $test;
And you overwriting the $test variable.
including files is one of the methods to split and order logic in your project.
In some matters it provides performance benefits, when you include files only when you need them, and saves you from code duplication.
As for databases, it doesn't matter when or how you connect to it, often database connection related logic is held by separate file, just because it's easier to edit and mantain, similar to config files.
Also consider this part of code:
$PRODUCTION = 0; // defines if we are at home computer or at work
if( $PRODUCTION == 0 )
include ("connect_home.php");
elseif( $PRODUCTION == 1 )
include ("connect_work.php");
else
die("Oops, something gone wrong."); //don't connect if we are in trouble!
I have some 2000 lines of code in php... some place i have some echo here and there to know that this or that is done properly... but i what more tracing and echo on each and every task done... i what echo in function enter and before function return... but all these echo polluate the code and the screen, and it's a nigtmare to remove or comment it out when ready for production...
the question, how to say, echo this and that, but when i say debug off, stop echo..how do you that in your code... what i tought was
global $debug_echo;
$debug_echo = true;
if ($debug_echo) {echo "function xyz - start";}
if ($debug_echo) {echo "function xyz - end";}
...
...
so with this, i can turn debug everywhere with one change... does it make sense ?
Best thing to do for debugging, would be to use a constant. So at the top of your file, or wherever you want to declare debug mode on or off you do this
define('DEBUG', true);
Then to check you just want to do
if(DEBUG === true) { echo 'something here'; }
You only have to declare the constant once, and then it's available throughout the entire scope of the code, providing it's declared somewhere.
In a core file, such as config.php (which is usually included in all sub files in a web app), have something similar to this:
$debug = true;
if($debug){
ini_set('display_errors', 1);
error_reporting(E_ALL);
}else{
error_reporting(0);
}
then just change true to false to disable debugging.
If you want to dump errors in the middle of scripts, such as MySQL queries, then that's a whole different kettle of fish.
Check out this slideshow: http://www.slideshare.net/asgrim1/errors-exceptions-logging-php-hants-oct-13
Here is something you can do:
function is_dev() {
return isset($_SERVER['APP_ENV'] AND $_SERVER['APP_ENV'] === 'DEV')
}
..In your apache virtual host configuration / .htaccess
SetEnv APP_ENV DEV
If you are using a framework or creating your SQL queries dynamically, you can output them if is_dev() returns true.
if(is_dev()) {
echo $sql;
}
I want to call function in PHP i use require_once for this ....
now i want to call the function in a particular event how it is done ?
For example I have an function
function validateHostName($hostName) {
if ((strpbrk($hostName, '`~!##$^&*()=+.[ ]{}\\|;:\'",<>/?') == FALSE) && !ctype_digit($hostName) && eregi("^([a-z0-9-]+)$", $hostName) && ereg("^[^-]", $hostName) && ereg("[^-]$", $hostName)) {
return true;
}
else return false;
}
in error-status.php file .. (external file)
now i want to cal this file in 2.php in between here
else if ($apply == "Add") {
//$length=$doc->getElementsByTagName('Server')->length;
if ($addntp == "") {
seterror("0:|: :|: Add NTP server");
//$error_text="NTP server already added"."\n"."Exiting ...";
//AddLog("timeconfig.php",$error_text,ERR_INFO);
header("Location: datetime.php");
exit;
}
can u help me fast ... how it is call ... i use require function for include ....
use this .. for a function call
include 'error-status.php';
validateHostName('myhostname');
I believe you can do that with changing some settings on your server. I never thought of including a php file which is on another domain.
You could use open_dir = on, and include the file with using exact path of it.
Let's say this is the domain where you have external.php to include; www.anotherdomain.com
Let's say this is the domain where you want to include external.php from www.anotherdomain.com to www.yourdomain.com
You have to have open_dir feature on, on www.anotherdomain.com. Later you can include any file from www.anotherdomain.com to any of your domains.
The point here is to have both domains hosted on the same server.
Once again, never tried or had the need to include an external php file if I didn't code it (I included files which is on another domain with using open_dir). If it is possible this would be dangerous anyway.