Reputation: 89
I have a file which is session secured "admin.php". This is default page after login. This throwing an error given below:
Fatal error: Call to undefined function listPages() in C:\xampp\htdocs\cd-website\cms\admin.php on line 14
<?php
/*
* initialize session for admin
*/
session_start();
if(isset($_SESSION['admin_user']))
{
require_once '../cms/config.php';
$action = isset($_POST['action']) ? $_POST['action'] : '';
if ($action == null)
{
listPages();
exit;
}
/*
* using switch for choosing function
*/
switch($action)
{
case 'ManagePages':
ManagePages();
break;
case 'listUsers':
listUsers();
break;
case 'orderList':
listOrders();
break;
case 'listBanner':
listBanners();
break;
case 'NewsletterUser':
NewsletterUsers();
break;
case 'Newsletter':
Newsletter();
break;
case 'listQuestion':
listquestions();
break;
case 'testinomial':
listTesti();
break;
default:
listPages();
}
/*
* different function for different tasks
*/
function ManagePages()
{
listPages();
}
function listUsers()
{
// include listusers.php here. required rows is in listusers.php file
}
function listOrders()
{
// include listorders.php here
}
function listBanners()
{
// include listbanners.php here
}
function NewsletterUsers()
{
// include listNUsers.php here
}
function listquestions()
{
// include listquestions.php here.
}
function listTesti()
{
// include listTesti.php here.
}
function Newsletter()
{
// include newsletter.php
}
function listPages()
{
// include listPages.php here
}
}
else
{
header("Location:index.php");
}
When i try to solve error on line 14, it shows error on every line where listPages()
exist.
Why it so? Please help!
Upvotes: 1
Views: 12745
Reputation: 196
Check if Your function is set before calling. In PHP You have to declare functions before calling them.
Your error tells clearly that when You call listPages()
this function doesn't exists yet.
I'm sure if You place just after:
if(isset($_SESSION['admin_user']))
{
Declaration of this function
function listPages()
{
//Do something here
}
Your error will disapear.
What i can see You are trying to use functions in structural code as they would be in some class (object oriented code) and unfortunetly PHP dosen't work that way.
Upvotes: 0
Reputation: 12410
You declared a function inside a if
block, which can be tricky. According to http://www.php.net/manual/en/functions.user-defined.php, conditional functions, they will not be available until the execution reaches the function definition, but if you place the function outside, the sequence doesn't matter and the function is available throughout the whole script.
<?php
bar(); // OK
function bar()
{
echo "I exist immediately upon program start.\n";
}
if (TRUE) {
foo(); // Fails because `foo` isn't defined yet.
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}
And please check http://codepad.org/EMW3kzqC.
So the solution is to declare the function outside the if
block, or place them above the location where you use it (if it really needs to reside inside the if
block).
Upvotes: 10
Reputation: 2493
<?php
/*
* initialize session for admin
*/
session_start();
if(isset($_SESSION['admin_user']))
{
require_once '../cms/config.php';
$action = isset($_POST['action']) ? $_POST['action'] : '';
if ($action == null)
{
listPages();
exit;
}
/*Functions */
function ManagePages()
{
listPages();
}
function listUsers()
{
// include listusers.php here. required rows is in listusers.php file
}
function listOrders()
{
// include listorders.php here
}
function listBanners()
{
// include listbanners.php here
}
function NewsletterUsers()
{
// include listNUsers.php here
}
function listquestions()
{
// include listquestions.php here.
}
function listTesti()
{
// include listTesti.php here.
}
function Newsletter()
{
// include newsletter.php
}
function listPages()
{
// include listPages.php here
}
/*
* using switch for choosing function
*/
switch($action)
{
case 'ManagePages':
ManagePages();
break;
case 'listUsers':
listUsers();
break;
case 'orderList':
listOrders();
break;
case 'listBanner':
listBanners();
break;
case 'NewsletterUser':
NewsletterUsers();
break;
case 'Newsletter':
Newsletter();
break;
case 'listQuestion':
listquestions();
break;
case 'testinomial':
listTesti();
break;
default:
listPages();
}
/*
* different function for different tasks
*/
}
else
{
header("Location:index.php");
}
Upvotes: -1
Reputation: 415
I'm not exceptionally familiar with php, but it looks like you're defining your functions only within the big if block there. You might be running into the way php handles scope.
Try placing your function definitions outside of the if block and see what happens?
Upvotes: 0
Reputation: 1068
here two solutions are ther one is using the Class, above method if you are using the above method the function should be defined before calling that function.
so
function listPages()
{
//include listPages.php here
}
after that call listPages()
Upvotes: 0