unable to use variables defined in class - php

Hi i am trying to import the configuration variables from a config file that i had created
Config.php has
class Config
{
public $SERVERNAME = '*******';
public $SUSERNAME = '*********';
public $SPASSWORD = '*********';
public $DBNAME2 = '********';
}
for importing this variable in another file i tried
include('./Config.php') or die("error");
$cnfig = new Config();
But when i try to use $cnfig->DBNAME2 its not working
Please help to sole this problem.
Thank you.
EDIT
It was working in localsystem , when i tied to upload through web hosting it was not working.

Try below one
include ('./Config.php');
$cnfig = new Config();
echo "<pre>";print_r($cnfig);
Edit:
I am getting below output so it should be working for you too.
Config Object
(
[SERVERNAME] => *******
[SUSERNAME] => *********
[SPASSWORD] => *********
[DBNAME2] => ********
)
Edit:2
You can also do like this
(include('./Config.php')) or die("error");
$cnfig = new Config();
echo "<pre>";print_r($cnfig);
http://www.php.net/manual/en/function.include.php#39043
or has higher priority than include()
Error Ouput:
When only use include('./Config.php') or die("error"); it's giving below error.
Warning: include(1) [function.include]: failed to open stream: No such file or directory in D:\path_to_file\myConfig.php on line 2
Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.;D:\ZendServer\share\ZendFramework\library') in D:\path_to_file\myConfig.php on line 2
Fatal error: Class 'Config' not found in D:\path_to_file\myConfig.php on line 6

Related

How to access database connection and base url in child folder?

I am working on XYZ project. In that project having admin, assets, class, controller, core folder and some .php files. In core folder having config.php and database.php files.
Here's config.php code:
<?php
include ("core/database.php");
spl_autoload_register(function ($class_name) {
include ('class/'.$class_name . '.php');
});
?>
and database.php code:
<?php
session_start();
define('BASE_URL', 'http://localhost/XYZ/');
class database{
private $servername;
private $username;
private $password;
private $dbname;
protected function connect(){
$this->servername = 'localhost';
$this->username = 'root';
$this->password = '';
$this->dbname = 'xyz';
$conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
return $conn;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
}
}
?>
Here's index.php code:
<?php
include("core/config.php");
echo BASE_URL;
exit();
?>
In that page i got BASE_URL in defined in database.php
But when i try to get same BASE_URL in admin->index.php got some error following:
Warning: include(core/database.php): failed to open stream: No such file or directory in C:\xampp\htdocs\xyz\core\config.php on line 3
Warning: include(): Failed opening 'core/database.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\xyz\core\config.php on line 3
Notice: Use of undefined constant BASE_URL - assumed 'BASE_URL' in C:\xampp\htdocs\xyz\admin\index.php on line 3
BASE_URL
So, My query is how to access databse connection and BASE_URL in admin->index.php
#rajeev answer i got database connection and BASE_URL in admin->index.php
But i have one more query
In class folder having Test.php file the code is below:
<?php
class Test extends Database{
function __construct(){
date_default_timezone_set('Asia/Kolkata');
$date = date("y-m-d H:i:s");
$this->get_date = $date;
}
public function test(){
return $this->get_date;
}
}
?>
This class i want access in admin->index.php file but get error below
Warning: include(class/Test.php): failed to open stream: No such file or directory in C:\xampp\htdocs\technical\core\config.php on line 6
Warning: include(): Failed opening 'class/Test.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\technical\core\config.php on line 6
Fatal error: Class 'Test' not found in C:\xampp\htdocs\technical\admin\index.php on line 5
and i tried code in admin->index.php
<?php
include("../core/config.php");
$testClass = new Test();
echo $testClass->test();
?>
You can use "getcwd"(https://www.php.net/manual/en/function.getcwd.php) function to gets the current working directory. If the "class" folder isn't accessible in the current working directory, you will have to change your include path in the "config.php" file.

How to have more than one include on the same page in PHP

The title says it all, I've been trying to get this to work but it isn't working for me. It is just displaying the first include.
<?php include 'config.php'; include 'https://theurl/that_the_header_is_on/header.php'; include '$header_url'; ?>
EDIT: The error is here:
[11-Dec-2016 03:26:13 Europe/Moscow] PHP Warning: include(): http://
wrapper is disabled in the server configuration by allow_url_include=0
in /home/myurlf/public_html/SimplePages/index.php on line 4
[11-Dec-2016 03:26:13 Europe/Moscow] PHP Warning:
include(myurl.fluctis.com/SimplePages/Default/1.0/header.php‌​):
failed to open stream: no suitable wrapper could be found in
/home/myurlf/public_html/SimplePages/index.php on line 4 [11-Dec-2016
03:26:13 Europe/Moscow] PHP Warning: include(): Failed opening
This should make code run. But...
<?php
include('config.php');
include('https://theurl/that_the_header_is_on/header.php');
include($header_url);
?>
Are you trying to do this? .. your post is a little confusing
<?php
include('config.php');
$header_url = 'Includes/header.php';
include($header_url);
?>
Try this. This is what I currently using.
/**
* Load all the class under RTlib\RTphp
*
* #return boolean False if error
*/
function RTphpLoad() {
$lib = array();
$lib[] = '/src/RTmysqli.php';
$lib[] = '/src/RTpassword.php';
$lib[] = '/src/RTslugify.php';
$lib[] = '/src/RTutil.php';
try {
foreach ($lib as $class) {
require_once dirname(__FILE__) . $class;
}
return true;
}
catch (Exception $ex) {
trigger_error('RTphpLoad() Fail to load. ' . $ex->getMessage());
return false;
}
}
In another php file
require_once FILEROOT . 'RTlib/RTphp/load.php';
RTphpLoad();

Missing file error (XMLStream.php) using XMPP

I want to send message using XMPP and I have used this code:
include 'XMPP.php';
#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
#If this doesn't work, are you running 64-bit PHP with < 5.2.6?
$conn = new XMPPHP_XMPP('hostname',5222, 'panelusername', 'panelpassword', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->message('senderusername', 'This is a test message!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
But it gives me this error:
Warning: require_once(XMPPHP/XMLStream.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/xmppmessage/examples/XMPP.php on line 30
Fatal error: require_once(): Failed opening required 'XMPPHP/XMLStream.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/xmppmessage/examples/XMPP.php on line 30
In the XMPP.php file on line 30, change it from:
require_once 'XMPPHP/XMLStream.php';
to
require_once 'XMLStream.php';

Am I calling this class wrong?

I have this code in a file named server.class.php:
class server
{
function addPlayer($player)
{
//Stuff
}
}
Then, I have this in a file called send.php. These are lines 38-40:
require('/var/www/server/apply/server.class.php');
$server = new server;
$server->addPlayer($_POST['IGN']);
However, I get this error when I visit my page (Php.ini is set to show all errors):
Fatal error: Class 'server' not found in /var/www/server/apply/send.php on line 39
Line 39 is $server = new server;
What am I doing wrong? I have verified that the class file is in /var/www/server/apply/server.class.php.
$server = new server()
Missed the brackets

PHP Login: failed to open stream: No such file

Greeting's I'm currently taking my first attempt at developing a login for my website. For this login I'm utilizing PHP and MySQL. My problem is that I keep recieving the error below... I've tried exhausted all possibilities of how to rename "require_once" input to my knowledge, but still know luck.
Warning: require_once(C:\Users\Derek\Fall_2013\PS-WebDesign\includes\constants.php) [function.require-once]: failed to open stream: No such file or directory in D:\Hosting\11311638\html\classes\Mysql.php on line 3
Fatal error: require_once() [function.require]: Failed opening required 'C:\Users\Derek\Fall_2013\PS-WebDesign\includes\constants.php' (include_path='.;C:\php\pear') in D:\Hosting\11311638\html\classes\Mysql.php on line 3
Below is the line of code that is producing the Fatal error, My question is have I not called the file correctly or is there code my feeble mind is missing? The error is on the 3rd line "require_once...."
<?php
require_once 'includes/constants.php';
class Mysql {
private $conn;
function __construct() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die(
'You ***'d up.');
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM users
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}
The path either needs to be an absolute path to the file you want to include or you need to add its location to your include path.
http://php.net/manual/en/function.set-include-path.php
is this include file in the root directory?
You should also remove the blank line above the include line.
Lsatly, the syntax for die should read die(mysqli_error() 'You ***'d up.');

Categories