require_once() function unable to include functions() - php

index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}
Now when i go to index.php, it gives me this error:-
Fatal error: Call to undefined function abspath() in C:\xampp\htdocs\index.php on line 3

Hey all my commenting people were right... i don't know why it happened but when i restarted my xampp it worked fine for me :)
So this is 100% correct
index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}

Related

PHP Fatal error: Call to undefined function?

So i have a problem with my website when im hosting it on my webhost.
i get this error:
PHP Fatal error: Call to undefined function getSkillIcons()
The weird thing is that locally(Xampp) it works just fine.
This is how i included the function(in index.php):
<?php include 'http://sub.website.com/incl/Settings.php'; ?>
this is where i call it (index.php):
<div class="panel-body" style="text-align:center">
<?php getSkillIcons(explode(',', skills)); ?>
</div>
This how i defined skills (settings.php)
define("skills", "Test1,Test2,Test3,Test4");
this is the function itself: (settings.php)
function getSkillIcons($skills) {
echo '<img src="http://sub.website.com/incl/img/skill_icons/Overall-icon.png" class="skill-icon">';
for ($i = 0; $i < count($skills); $i++) {
$tooltip = 'title="'.$skills[$i].'" data-toggle="tooltip" data-placement="top"';
echo '';
}
Call to undefined function error clearly shows that its not getting your function where you have defined. Reason is you are attaching full path of settings.php file with http.
You need to include settings.php file without http path at the top of 'index.php' and make sure settings.php file is located in your project only. If it is located at the same folder with index.php, then simply include like below.
<?php include __DIR__.'/settings.php'; ?>
If your settings.php file is located in some other folder then you can use $_SERVER['DOCUMENT_ROOT'] to include that file like below:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/settings.php";
include_once($path);
Also just an FYI for anyone trying to call a function within the same class in PHP, refer to the function like below:
return $this->doSomething($str);

PHP require variable from php file

How can I make a config.php file with all environment variables and import it successfully into other scripts?
config.php:
<?php
$folder = 'SomePath';
?>
script1.php
<?php
require_once('config.php');
function doStuff(){
global $folder;
echo($folder);
}
doStuff();
?>
I get
PHP Notice: Undefined variable: folder in script1.php
It's php version 5.4.41 and I can't do anything about it (no root)
keeping it simple, if it was me:
//config.php:
<?php
$folder = 'SomePath';
?>
//script1.php
<?php
require_once('config.php');
function doStuff($folder){//parse the variable in to the function
echo($folder);
}
doStuff();
?>

Usage of class in PHP from different directory

I have been using Classes in PHP.
I have a directory, test, In which there is index.php
A sub-directory to test is new, which has checkuser.php
Code for checkuser CODE:
<?php
public class checkuser{
public function checkuser()
{
echo "This is class";
}
}
?>
Code for index.php:
<?php include('new/checkuser.php'); ?>
<html>
<head>
</head>
<body>
<?php
checkuser::checkuser();
?>
</body>
</html>
But It always throws error.
Please Help.
What kind of error is that?
There is more things:
You are declaring the class "public"
Parse error: syntax error, unexpected T_PUBLIC on line 2
You are calling non-static method statically
Fatal error: Non-static method checkuser::checkuser() cannot be called statically on line 17
You are including the file, without being aware of the context, i suggest this practice:
<?php include( dirname(__FILE__) . '/new/checkuser.php'); ?>
or if you are using PHP > 5.3
<?php include( __DIR__ . '/new/checkuser.php'); ?>
And here is how your code could work:
<?php
class checkuser {
public static function myfunction()
{
echo "This is class";
}
}
?>
<html>
<head>
</head>
<body>
<?php checkuser::myfunction(); ?>
</body>
</html>
http://codepad.org/C9boO1lI
Note: of course, you can split your code in more files, just make sure that you specify the path as seen above, so it can find the actual file

Include and class PHP

Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.

require_once of function file in a header

ive got an global include/header.php file like:
UPDATE
folder structure :
/
include/
header.php
functions.php
content/
show.php
include/header.php
<?php
require_once('functions.php');
$settings = blaa;
....
?>
include/functions.php
<?php
function hello()
{
echo "hello world";
}
?>
and now a content file like content/show.php
content/show.php
<?php
require_once('../include/header.php');
echo "show page want to say: ";
hello();
?>
and now if i look in the apache error_log call to undefined function in content/show.php on line...
i cannot find out why :-/
Greetings
If you tweak your header.php code to this:
<?php
define('INCDIR', str_replace('\\', '/', dirname(__FILE__)));
require_once(INCDIR . '/functions.php');
It wont matter where you include it from it will constantly find the right path to the include folder

Categories