This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
Fatal error: Cannot redeclare getIp() (previously declared in C:\xampp\htdocs\ecommerce\functions\functions.php:12) in C:\xampp\htdocs\ecommerce\functions\functions.php on line 21
This is the error I received when creating a checkout page
function getIp() {
$ip = $_SERVER['REMOTE_ADDR'];
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $ip;
}
this is the function it refers to
<?php
if(!isset($_SESSION['customer_email'])) {
include("customer_login.php");
}else{
include("payment.php");
}
?>
and it happens after this is not set function
probably you have included the file before, use include_once
<?php
if(!isset($_SESSION['customer_email'])) {
include_once("customer_login.php");
}else{
include_once("payment.php");
}
?>
or you can check if function exists with function_exists:
if(!function_exists("getIp")) {
// declare your function
} else {
// it already exists, do something else
}
Related
i have php mvc project .
This is my router class.
router class job is include controller in main index page of website.
its work on localhost but when i uploaded on my host not working
what i do ?
<?php
class route
{
function __construct()
{
if(empty($_GET['url']))
{
$url = "index";
}
else
{
$url = $_GET['url'];
}
$url = explode("/",$url);
if(!empty($url[0]))
{
$controllername = "controllers/".$url[0].".php";
if(file_exists($controllername))
{
require($controllername);
$object = new $url[0];
//$object->loadmodel($url[0]);
if(!empty($url[1]))
{
if(method_exists($object,$url[1]))
{
if(!empty($url[2]))
{
$object->$url[1]($url[2]);
}
else
{
$object->$url[1]();
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
}
}
this is my error in error_log
thrown in /home/mogeir/public_html/libs/route.php PHP Notice: Array
to string conversion in /home/mogeir/public_html/libs/route.php on
line 33 PHP Notice: Undefined property: admin::$Array in
/home/mogeir/public_html/libs/route.php on line 33 PHP Fatal error:
Uncaught Error: Function name must be a string in
/home/mogeir/public_html/libs/route.php:33
Use the following function call forms instead - the commented ones are correct alternatives too. See call_user_func and call_user_func_array.
if (!empty($url[2])) {
$object->{$url[1]}($url[2]);
// Alternative 1
// call_user_func(array($object, $url[1]), $url[2]);
// Alternative 2
// call_user_func_array(array($object, $url[1]), array($url[2]));
} else {
$object->{$url[1]}();
// Alternative 1
// call_user_func(array($object, $url[1]));
// Alternative 2
// call_user_func_array(array($object, $url[1]), array());
}
You said:
its work on localhost but when i uploaded on my host not working what i do ?
That has to do with the error reporting activation or with difference in PHP version.
I have this function called checkCentre1() and I'm unable to open my web application due to below error appearing:
Fatal error: Cannot redeclare checkCentre1() (previously declared in C:\xampp\htdocs\scms2\protected\views\layouts\main.php:39) in C:\xampp\htdocs\scms2\protected\views\layouts\main.php on line 50
I did not declare checkCentre1() somewhere else. I have tried few solutions below but none is working:
if (!function_exists('checkCentre1')) { //function code }
include_once
My code is as below:
function checkCentre1($value){ //line 39
$modelUser = AuthUsers::model()->findByPk($value);
if($modelUser){
if($modelUser->ref_user_type_idref_user_type == 1 || $modelUser->ref_user_type_idref_user_type == 2){
return true;
}else{
return false;
}
} else
return false;
} //line 50
Does anybody has any other solution for this?
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
How should I call this function? I'm new to PHP. Here's my code... but I have an error
Notice: Undefined variable: ip in C:\xampp\htdocs\PHPTest\ip.php on line 19
<?php
function getRealIpAddr(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
call_user_func('getRealIpAddr', '$ip');
echo $ip;
?>
UPDATE
Strange reason, I'm using Windows 10, localhost, xampp and google Chrome this script doesn't provide me an ip address! That's why a corrected code was empty... Thought it was some kind of errors or something
Second UPDATE
If you're getting no ip like me, you may try this solution on httpd.conf
Your function returns the IP address, so assign a variable to the return value of the function, like so:
$ip = getRealIpAddr();
Error is very clear $ip is not defined:
Modified Code:
<?
function getRealIpAddr(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getRealIpAddr(); // your function
echo $ip;
?>
If you want to use $ip variable that you defined inside the function than note that scope of the $ip is limited into the function.
You can not call this variable outside the function. for this you need to store it in a variable as like above mentioned example ($ip = getRealIpAddr();).
You can't access to a variable declared inside a function, but with the return statement you can do :
echo getRealIpAddr();
Instead of :
call_user_func('getRealIpAddr', '$ip');
echo $ip;
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm getting an error as:
Parse error: syntax error, unexpected end of file in ...\system\core\Loader.php(829) : eval()'d code on line 307..
this is code in loader.php
foreach ($this->_ci_model_paths as $mod_path)
{
if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
{
continue;
}
if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
{
if ($db_conn === TRUE)
{
$db_conn = '';
}
$CI->load->database($db_conn, FALSE, TRUE);
}
if ( ! class_exists('CI_Model'))
{
load_class('Model', 'core');
}
require_once($mod_path.'models/'.$path.$model.'.php');
$model = ucfirst($model);
$CI->$name = new $model();
$this->_ci_models[] = $name;
return;
} // this line number 307
and eval code
if ((bool) #ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
{
echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));//this is line number 829
}
else
{
include($_ci_path);
}
I tried searching for this but couldn't find anything.
This will be caused by a File you have written.
It looks like you might be loading a model with a missing { or } or some syntax error. So you need to determine what models you are loading.
You can do that from the URL to see what controller/method you are calling. Check the controllers constructor to see if you are loading any models there... Check the Method and also check your autoloader.
Then check those models - the ones you have written - and look for errors. If you are using a decent editor or IDE, you should see where they are occurring.
This question already has answers here:
PHP error - cannot redeclare function [duplicate]
(5 answers)
Closed 9 years ago.
After I have instaled in my site one script, I have an error:
Fatal error: Cannot redeclare ae_detect_ie() (previously declared in /home/xdesign/public_html/Powerful/config.php:24) in /home/xdesign/public_html/Powerful/config.php on line 29
This is the line:
function ae_detect_ie()
{
if (isset($_SERVER['HTTP_USER_AGENT']) &&
(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
return true;
else
return false;
}
I don't understand what I did wrong!
The site: http://fbswapes.com
The same script is working in another host.
Simpily you have declared a function twice.. Example:
Global.Fun.php
<?php
function Do_Something (){
echo "This Does Something";
}
?>
Index.php
<?php
include "Global.Fun.php";
function Do_Something($Arg){
echo "Argument Supplied".$Arg;
}
?>
Notice, I have declared the same function twice, one in my global.fun.php page and again in the index.php page..
If you are in doubt that a function is currently set:
if (function_exists('Do_Something')){
echo "Function Exists";
}else{
echo "Function Not Found, This name Can be used!";
}