Fatal error: Call to undefined function - php

I am trying to call a function which is defined in functions.php. But I can’t figure out why this error is coming.
Let me tell you in detail what I am trying to accomplish: there are two header files — client and admin. The client header will be accessed when the user enters into a page through index.php, but admin header will be accessed after an admin successfuly logged in.
In the functions.php this is the function I have defined:
function get_my_header(){
if(is_home()){
get_header('client');
}
elseif(is_page('dashboard')){
get_header('own');
}
}
from index.php get_my_header() is called with no error but when I try to access the same function from the admin page I am getting this error:
Fatal error: Call to undefined function get_my_header()
The page where this error is coming has only this one line:
<?php get_my_header(); ?>

The most common causes of this are:
The file you think is being included, isn't being included.
Your calling the function prior to including the file
Based on your description as a header a file - I think the first is most likely. As you also mention that it's in an admin page that it doesn't work on - does the admin page live in a sub directory e.g:
httpdocs/
index.php
admin/
admin_index.php
If it does - the file path to the include file may be wrong, depending on your error_reporting() settings you may or may not be alerted to this.
One way to debug this would be to use require() instead of include() on the functions file - as that creates a fatal error if PHP can't find the script.
http://php.net/manual/en/function.require.php

Related

Getting "Fatal error: Call to undefined function site_url()" after redirect

I am creating a WordPress Plugin and I am using a wp_redirect() on one page to go to another.
On the first file, I use wp_redirect(), which takes a URL as an argument, built using site_url().
After the redirect, on the second file, if i try to call site_url(), or wp_redirect(), or home_url(), I get messages like this: "Fatal error: Call to undefined function site_url()...", the same for wp_redirect() and home_url().
It seems that these functions are not recognized.
I mention that these 2 files are in the same directory.
Can you help me, please?
Thanks!
If it helps anyone, i managed to make it work.
I included "wp-load.php" in the second file, and now it works fine.

wordpress prevent error reporting

i'm making a custom theme and noticed one thing. when i try to go directly to my theme index.php file through url (or anypage) for example:
localhost/wordpress/wp-content/themes/my-theme/index.php
this error will appear.
Fatal error: Call to undefined function get_header() in ... on line...
i've tried to download and activate other themes but the result is the same.
how can i prevent this from happening?
i've tried adding the following lines to the wp-config.php
error_reporting(0);
#ini_set('display_errors', 0);
but with no results.
i've also tested it "live" but with no results.
thank's in advance.
Correct me if I'm wrong but that error occurs when someone tries to directly access the theme's index.php.
For example when a bot tries to access it.
You should never access it directly like this:
<?php get_header(); ?>
But rather try it like this:
<?php
if (function_exists('get_header')) {
get_header();
} else {
// Here goes whatever you want to do when the file is accessed directly.
}
?>

object not found error 404-codeigniter framework-xampp server

Iam new to codeigniter framework and xampp server.Iam getting object not found error
when i click on fivespecies it is displaying error message
The welcome.php controller is your default, and when you don't declare a function name within the controller, it loads the index() function, so loading http://localhost/antimalarial/ loads the index function in your welcome controller.
However, if you want to load any other function within the welcome controller, you will need to provide the controller name AND the function name.
Your link should be http://localhost/antimalarial/welcome/fivespecies
Your might need to write your URL as: http://localhost/antimalarial/Welcome/fivespecies
If this link does not work, try this:
http://localhost/antimalarial/index.php/Welcome/fivespecies
This case is valid if you haven't already set the .htaccess file.
If you want to remove index.php from your url. Please download and add .htaccess file in your root directory. Click here to download the file.
Hope this helps.

Fatal error: Call to undefined function redirect_to()

very new to this, so I apologize in advance for rookie mistakes.
I am currently working through one of the PHP/MySQL introductory series from lynda.com. I am loving it thus far, and have been having success, but this one's got me confused.
I have created a form for user input, called new_subject.php. The following is called create_subject.php, and it meant to process the form data from new_subject.php. Please keep in mind that I have not yet added any code to do this, I am just testing a redirect function:
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<?php
if (isset($connection)) {mysqli_close($connection);}
?>
The point of the redirect_to function is to redirect the user to the form at new_subject.php if they were to type in create_subject.php manually in the browser. Here is what redirect_to function looks like in functions.php:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
I am getting the following error when trying to get to create_subject.php manually:
Fatal error: Call to undefined function redirect_to()
The tutorial video says that I can either turn on output buffering, which I have tried to do in my php.ini file, but the error remains the same. The video says I can do that, or "fix the white space issue." I have no idea what this means.
I would appreciate any info on what this "white space issue" is, and if there is anything else I can do here. As far as I can see, I should be able to call this function without issues.
Thanks
for the whitespace issue, there must be no output prior to calling location header, in your code.. there must be no whitespace outside the php tags..
just rewrite so that all those are enclose in a single php tag and make sure there is no space outside of that, even a single one.
<?php
require_once("../includes/db_connection.php");
require_once("../includes/functions.php");
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
if (isset($connection)) {mysqli_close($connection);}
?>
read here.. How to fix "Headers already sent" error in PHP
however, your main issue is the function is not found..
check your functions.php and make sure redirect_to is define properly. if that is the case, then most likely issue is in your "require_once" must be pointing to a non existent file. can you tell us your folder structure?
your functions.php must be located in a folder called includes located outside the folder where your create_subject.php is located.
if the 'includes' folder is located inside the folder where create_subject.php is, then change your require_once to
require_once("includes/db_connection.php");
require_once("includes/functions.php");
if both are located in the same folder, then change your require_once to
require_once("db_connection.php");
require_once("functions.php");

Not able to access class object in php file loaded via ajax

I am running into a little problem with the below, I am trying to learn for some future projects and need a hand.
Here is the setup:
I have a user class in php in a models directory.
I have a home.php file in my root directory.
I have included the model for the user class in the home.php file and am able to access it just fine.
I have a profile.php file that I would like to load via ajax onto the home.php file.
I am able to load the profile.php file just fine, however the problem is that the info on that page which is just being called via <?php echo $user->first_name; ?> is not being displayed when I load the page via ajax. However if I load the page normally without ajax, it displays just fine.
What I have tried so far:
I have tried to include the user class in the profile page, however it is giving me an error that it is already included.
I would rather not place this into a session variable as I would like to be able to access the class from here.
I also need to be able to access the user class on other screens, that is why I have included it in the home.php file.
Any help would be greatly appreciated as I am rather new to working with php and ajax.
Thanks
Here is some of the code that I am working with.
home.php
//Here we include are models that we need access to.
include 'apps/user/model/user.php';
$my_user = new User();
global $user;
$user = $my_user->getUserById($_SESSION['loggedin_user_id']);
profile.php
<input type="text" value="<?php echo $user->username; ?>" class="input-xlarge">
javascript to load the form via ajax
$.ajax({
url: 'apps/user/view/profile.php',
success: function(data){
if(data != null) $("#content").html(data);
}
});
I also get the following error when I view the console.
[01-Jun-2013 10:38:03] PHP Notice: Undefined variable: user in /Users/btackett1377/development/Test/Base/apps/user/view/profile.php on line 23
Have you tried using require_once instead of include? I would try requiring the user class in both the home and profile pages.
For your reference, PHP has four functions for including files:
include - Blindly include the requested file with no regard for whether it's been included already. Warns you if the file is not found.
include_once - Ensure that the file is only included once. Also warns you if the file is not found.
require - Similar to include, but causes fatal error if the requested file could not be found.
require_once - Similar to include_once, but causes fatal error if the file is not found.

Categories