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.
Related
i have a connection file
file name inc.server.php
<?php
function db_name() { return 'dbname'; }
function db_user() { return 'username'; }
function db_pass() { return 'pw'; }
$koneknodatabase = mysqli_connect('localhost:2020',db_user(),db_pass(),db_name());
function close_Con() {
mysqli_close(mysqli_connect('localhost:2020',db_user(),db_pass(),db_name()));
}
?>
this file save in the my server with IP : 10.2.60.2
but when i require that file from my local pc
with
require('http://10.2.60.2/inc.server.php');
global $koneknodatabase;
$select = mysqli_query($koneknodatabase,"select from data");
$data = mysqli_fetch_array($select);
iam run that script on my localhost
but the result is
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
i have change allow_url_include to ON from php.ini
please help
You can't access the source code of a PHP script of a remote host like that, otherwise everyone would open the config.php file from every webpage they know (and they know the file exists) and get any login data they want.
When you open the URL http://10.2.60.2/inc.server.php in your browser you see the content someone outside of the host "10.2.60.2" will see. It will be most likely an empty page since your inc.server.php file doesn't generate any output (and shouldn't). So your other PHP host will include just an empty file from his point of view, which means there aren't any functions like db_name() defined or any variables like $koneknodatabase.
There are different ways on how separated hosts can communicate to each other, but thats a different topic and might result in different questions. Luckily there are millions of informations out there about how two hosts can communicate with each other.
If you're familiar with SMF, this is how you normally use its server side include:
//foo.php at http://example/foo.php
<?php
require('./SSI.php'); //assuming we're at SMF's root
//...
?>
But it's hidden to the untrained eye that accessing http://example/foo.php?ssi_function=something will cause ssi_something to be called inside SSI.php, effectively bypassing the foo.php's normal behaviour.
I could prepend this before require, but I could avoid a redirection:
if(isset($_GET['ssi_function']))
{
unset($_GET['ssi_function']);
return header('Location: ?' . http_build_query($_GET));
}
I have already opened an issue on GitHub, but what other options do I have to counter this nuisance?
As you mentioned, this is an implementation dependent behavior within SMF. You don't need to do a redirection in this case, as the $_GET superglobal is mutable, simply removing the ssi_function parameter should be enough.
This bug has been fixed in #4038.
## -177,6 +177,9 ##
// Have the ability to easily add functions to SSI.
call_integration_hook('integrate_SSI');
+// Ignore a call to ssi_* functions if we are not using SSI.php
+if (empty($modSettings['allow_ssi_functions_anywhere']) && isset($_GET['ssi_function']) && basename($_SERVER['PHP_SELF']) !== 'SSI.php')
+ unset($_GET['ssi_function']);
// Call a function passed by GET.
if (isset($_GET['ssi_function']) && function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
{
I want to run an external script, within the current script (for tidyness purposes) based on a series of if and else if statements.
Essentialyl what I want do is this...
if ($business_type == "Restaurant"); {require 'scripts/php/insert.php'}
else if ($business_type == "Hotel"); {require 'scripts/php/insert-hotel.php'}
Is this i the right way to call this external script?
That is the proper way of calling the scripts, but your syntax is just a little off :)
if ($business_type == "Restaurant"){
require 'scripts/php/insert.php';
}
else if ($business_type == "Hotel"){
require 'scripts/php/insert-hotel.php';
}
You might want to look into the documentation for Require and Include, but require is perfectly fine for what you're doing here.
I am using PHP Include:
<?php include 'file1.php'; ?>
i want to only include the first few lines of file1.php - is this possible?
If you really want to include (run as PHP), then just pull those lines out into a new file:
new.php:
<?php
// line 1
// line 2
And include it in both files:
existing.php and other.php:
<?php
include('new.php');
...
<?php
$return_from_inc = include('file1.php');
?>
file1.php
<?php
if ($x === 1) { return 'A'; }
else { return 'B'; }
//... return ("break") running script wherever you want to
?>
Depending on the content of those first lines, why don't you use PHP Functions?
file1.php
<?php
function what_i_want_to_include(){
//"First lines" content
}
}
existing.php
<?php
include('file1.php');
what_i_want_to_include();
?>
Using functions it's the simplest way to do it.
You can simply use return on the line of your choice and control will be sent back to the calling file.
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.
Source: PHP Manual
There are a few options to achieve this, but let me stress out that if this is necessary for your application to work you should really consider reviewing the app design.
If you want it programatically you can either grab the first x lines and use eval() to parse them. Example:
$file_location = '/path/to/file.php';
$number_of_lines = 5; //
$file_array = file($file_location);
if(!$file) {
return false; // file could not be read for some reason
}
$first_lines = array_slice($file_array, 0, $number_of_lines);
$to_be_evaluated = implode('', $first_lines);
eval($to_be_evaluated);
But you should take not that eval expects a string without the php opening tag (<?php), at least, not at the start. So you should search for it and delete it in the first line (if present):
if(strpos($first_lines[0], '<?php') !== false) {
$first_lines[0] = substr(strpos($first_lines[0], '<?php') + 5);
}
Another, and better option, and as suggested above, just pull out the required lines, save them to another file, and include them in both. You could also do this programatically, you could even extract the needed lines and save them to a temporary file on the fly.
Edit it is a 'weird' question, in the sense that it should not be necessary. Could you explain what exactly you are trying to do? Most probably we can come up with a nice alternative.
Edit
As I understand it correctly you have in the file-to-be-included a lot of stuff, but only the database settings are needed. In that case, put them elsewhere! Example:
settings.php
$connection = new mysqli($host, $user, $pass, $db);
if($connection->connect_error) {
die('This failed...');
}
header.php
<?php require_once('settings.php'); ?>
<html>
<head>
<title>My awesome website</title>
... other stuff
</head>
other_file.php
<?php
require_once('settings.php');
$r = $connection->query('SELECT * FROM `my_table` WHERE `random_field`=`random_value`');
etc. etc.
In settings.php you could also put everything in functions to ensure pieces are only executed when needed. You could in example create a get_connection() function, which checks if a database connection exists, otherwise creates it and returns it for usage.
No need for fancy eval() functions at all!
Please bear in mind that it isn't a crime to divide your application in a thousand files. It really isn't!
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