How should I call this function? PHP [duplicate] - php

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;

Related

Laravel/PHP error: Undefined variable: email2 [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
i was trying to send an email and when i want to whrite the email address of the destination (the email is a parameter of the function), i have this error. Can someone help me.
this is my function:
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
this is my error: "Undefined variable email2"
thank you
When you use anonymous function to pass variables you should use use construction
public function email($name, $email2) {
$data = array('name'=> $name);
Mail::send('mail',$data,function($message) use ($name, $email2) {
$message->to($email2, $name)->subject('Deposit Confirmation');
$message->attach(public_path('document.pdf'));
$message->from('xxx#xxx.pt', 'XXX');
});
}
Here you can find more examples about anonymous functions https://www.php.net/manual/en/functions.anonymous.php
Here simple example from official documentation
$message = "hello";
// Inherit $message
$example = function () use ($message) {
var_dump($message);
};
$example();
// Output: string(5) "hello"

undefined variable when Adding Dynamic Data to the View from database query in codeigniter [duplicate]

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 4 years ago.
I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
<h1><?php echo $data;?></h1>
through controller,
$this->load->model('loginmodel');
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
but when i execute this i get,
A PHP Error was encountered Severity: Notice
Message: Undefined variable: data
Filename: admin/account.php
Line Number: 11
Just use $query instead of $data in the view
MODEL
public function get_last_ten_entries()
{
$query = $this->db->query("SELECT username FROM public");
return $query->result();
}
CONTROLLER
$this->load->model('loginmodel');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$login_id = $this->loginmodel->login_valid($username, $password);
if($login_id){
$this->load->library('session');
$this->session->set_userdata('user_id','$login_id');
$data['query'] = $this->loginmodel->get_last_ten_entries();
$this->load->view('admin/account', $data);
}
VIEW
<?php
foreach($query as $row){
echo $row['name'];
}
?>

Fatal error: Cannot redeclare a function previously declared PHP [duplicate]

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
}

Cannot spot the issue with PHP Index error-possible multiple threads issue [duplicate]

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.
I'm struggling to find the problem with the following piece of PHP code:
The code is as follows
class WorkerThreads extends Thread
{
private $from_list;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
} else {
echo "NO FROM <br/><rb/>";
}
The following error occurs:
Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198
FROM: Viva-
The offending line 200 is
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
There seems to be a problem with the array
This will work perfectly fine if you don't use multiple threads.
I think many threads are trying to modify and read the same list. Try synchronizing the modification/read of the from_list. We may use a mutex to synchronize read/write to the from_list.
Check the code below:
<?php
class WorkerThreads extends Thread
{
private $from_list;
private $mylock;
public function __construct($x,$host,$users_email,$pass,$inbox)
{
$this->from_list = array(); # holds the unique froms extracted from headers
$this->mylock = Mutex::create();
}
public function __destruct()
{
Mutex::destroy($this->mylock);
$this->mylock = null;
}
public function run()
{
# Get Froms
if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
$from = trim(str_ireplace("From: ", "", $matches[0][0]));
Mutex::lock($this->mylock);
if (!array_key_exists($from, $this->from_list)) {
$this->from_list[$from] = 1;
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
}
Mutex::unlock($this->mylock);
} else {
echo "NO FROM <br/><rb/>";
}
}
}
?>

view file application/views//view..php not found [duplicate]

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.
public static function view($name, array $vars = null){
if(preg_match('/\\\\/', $name)){
$view_data = explode('\\', $name);
if(count($view_data) == 3)
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.$view_data[1].DS.'view.'.$view_data[2].'.php';
else
$file = APP_PATH.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php';
}
else{
$file = APP_PATH.DS.'views'.DS.'view.'.$name.'.php';
}
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
else{
if(isset($vars)){
extract($vars);
}
require($file);
}
}
[21-Apr-2015 13:10:30 UTC] PHP Notice: Undefined variable: view_data in /home/realitycards/public_html/test/system/load.class.php on line 28
your variable $view_data only gets defined in the first if statement. It looks like in the if statement below that you are using $view_data even though it hasn't been set.
if(!is_readable($file)){
throw new Exception('view file application'.DS.'views'.DS.$view_data[0].DS.'view.'.$view_data[1].'.php not found.');
}
You either need to set $view_data in your else statement, or in the exception above, use the $file variable that you've already set:
if(!is_readable($file)){
throw new Exception('view file '. $file .' not found.');
}

Categories