This error is making me confused

I have a form, when its submitted to a PHP page to handle the results (get the data and store it) I get this error which I cant figure out

20180325T152639: scasp.lukesspot.com/add_provider.php 
PHP Fatal error:  Uncaught Error: Call to undefined function getRealIpAddr() in /hermes/walnaweb03a/b2451/moo.lukesspotcom/masterasp/php/add_provider_logic.php:28 
Stack trace: 
#0 /hermes/walnaweb03a/b2451/moo.lukesspotcom/masterasp/add_provider.php(16): require() 
#1 {main} 
  thrown in /hermes/walnaweb03a/b2451/moo.lukesspotcom/masterasp/php/add_provider_logic.php on line 28

But Heres the strange part, when I check out the page where the function is called

...
  $thumb = testInput($_FILES['Thumb']['name']);
	
  $phone = $phone1.$phone2.$phone3;
		
  $ip = getRealIpAddr();
...

The required file

<?php
function testInput($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
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;
  }
?>

has two functions, but if the error says nothing about the first one which is used before the error.
Doesnt that mean that the require() works as the first function seems ok.
Is there something im missing?

Is getRealIpAddr included before its call? This could also be an issue if it isn’t.

Yes it is, Is it verifiable since the other function is also used thats is the require()
herea a bitr of PHP which requires the functions.php page and the add_provider_logic.php page

session_start(); // Start the session

// Check to see whether the session ID is set or not
if(isset($_SESSION['id'])) {

	require('db/configPDO.php'); // Require the database connection
	require('php/functions.php'); // Require the functions.php file


	$id = testInput($_SESSION['id']); // Create and append a variable for the session ID
	

	// Check to see whether or not the form was submitted via post
	if($_SERVER['REQUEST_METHOD'] == 'POST') {
		require('php/add_provider_logic.php');
	}  else {
		//take the user to a page allowing him to login
		require('add_provider_form.php');
	}
} else {
//take the user to a page allowing him to login
require('login_register.php');
}

So, when I submit the form, which goes to this page. The require(‘php/vfunctions.php’) has to work since the error appears in the logic page.

What’s the code for add_provider.php? I’ve tested your code and it seems to be working as expected for me.

thanks, I dont want to be a code vampire cause im really trying to understand where I went wrong, but heres that file

session_start(); // Start the session

// Check to see whether the session ID is set or not
if(isset($_SESSION['id'])) {

	require('db/configPDO.php'); // Require the database connection
	require('php/functions.php'); // Require the functions.php file


	$id = testInput($_SESSION['id']); // Create and append a variable for the session ID
	

	// Check to see whether or not the form was submitted via post
	if($_SERVER['REQUEST_METHOD'] == 'POST') {
		require('php/add_provider_logic.php');
	}  else {
		//take the user to a page allowing him to login
		require('add_provider_form.php');
	}
} else {
//take the user to a page allowing him to login
require('login_register.php');
}
?>

So, when the page first loads, im taken to t he form, the error happens when I submit the form

Yeah. I know that much. I am trying to figure out why it’s giving you an undefined function error. It typically happens when either the function isn’t being required/included before the call or when the function doesn’t exist.

What’s all inside add_provider_logic.php?

here it is,

<?php
//echo 'FILES["Image"]<pre>';
//print_r($_FILES['Image']);
//echo '</pre>';

if($_SERVER['REQUEST_METHOD'] == 'POST') {

  $name = testInput($_POST['Name']);
  $email = testInput($_POST['Email']); 
  $phone1 = testInput($_POST['phone1']); 
  $phone2 = testInput($_POST['phone2']);
  $phone3 = testInput($_POST['phone3']); 
  $id = testInput($_POST['userID']);
  $method = testInput($_POST['Method']);
  $rate = testInput($_POST['Rate']);  
  $weight = testInput($_POST['Weight']); 
  $h_feet = testInput($_POST['HFeet']);
  $h_inches = testInput($_POST['HInches']);
  $waist = testInput($_POST['Waist']); 
  $cup = testInput($_POST['Cup']);
  $age = testInput($_POST['Age']); 
  $ethnicity = testInput($_POST['Ethnicity']); 
  $introduction= testInput($_POST['Introduction']);
  $thumb = testInput($_FILES['Thumb']['name']);
	
  $phone = $phone1.$phone2.$phone3;
		
  $ip = getRealIpAddr();
  


  $sql = "INSERT INTO providers
	(userID,name,email,phone,cMethod,rate,weight,hFeet,hInches,waist,cup,ethnicity,age,introduction,thumb,ip) 
	VALUES 
	(:id,:name,:email,:phone,:method,:rate,:weight,:hFeet,:hInches,:waist,:cup,:ethnicity,:age,:intro,:thumb,:ip)"; // SQL Statement
  $prepare = $conn->prepare($sql); // Prepare the SQL Statement

	// Create an array of named parameters to bind and execute
  $parameters = array(
	':id' => $id,
	':method' => $method,
	':rate' => $rate,
	':weight' => $weight,
	':name' => $name,
	':email' => $email,
	':phone' => $phone,
	':waist' => $waist,
	':hFeet' => $h_feet,
	':hInches' => $h_inches,
	':age' => $age,
	':cup' => $cup,
	':intro' => $introduction,
	':ethnicity' => $ethnicity,
	':thumb' => $thumb,
	':ip' => $ip,
  );
		
	if($prepare->execute($parameters))	{	
		
	  $providerID = $conn->lastInsertId();
  
	  $upload_directory = 'images/providers/'.$providerID;
				
	  mkdir($upload_directory);
	  mkdir($upload_directory."/del");	  
	  
	  $sql = "UPDATE users SET providerID = :providerID WHERE id = :id"; // SQL Statement
  
	  $prepare = $conn->prepare($sql); // Prepare the SQL Statement

	  // Create an array of named parameters to bind and execute
	  $parameters = array(
		':providerID' => $providerID,
		':id' => $id,
		);
		if($prepare->execute($parameters)) {
			
		  //echo "users table updated, providerID = .".$providerID." for user ".$id;
  
		  $total = count($_POST['Options']);
			   
		  for($i = 0; $i < $total; $i++) {
				   
		  $sql = "INSERT INTO providers_options (optionName, providerID) VALUES (:option,:providerID)"; // SQL Statement
			   
		  $prepare = $conn->prepare($sql); // Prepare the SQL Statement
  
		  // Create an array of named parameters to bind and execute
		  $parameters = array(
			':providerID' => $providerID,
			':option' => $_POST['Options'][$i],
		  );
		  $prepare->execute($parameters);
		  }
		}
	} else {
	  header('Location: provider_exists.php');
	  die();
	}
	//upload featured pic
			 
	if(isset($thumb)) {
		if($thumb != '') {
	

		  //echo 'Upload Diectory: '.$upload_directory;

		  $tmp = $_FILES['Thumb']['tmp_name']; // Create and ppend a variable to tmp_name
		  $image = $thumb; // Create and append a variable to tmp_name		  
		 
		  require('test_image.php');
				
		  //echo '<br>Featured Pic: '.$tmp.' => '.$file;
					
		  move_uploaded_file($tmp, $file);
		}
	}
	//upload pictures
			 
	if(isset($_FILES['Image']['name'][0])) {
		if($_FILES['Image']['name'][0] != '') {

		  $total = count($_FILES['Image']['name']);
			

		  //echo '<pre>';print_r($_FILES['Image']);echo '</pre>';
		  //echo '<br>Count of Images: '.$total.', to be uploaded!<br>';

		  // Loop through each file
		  for($i = 0; $i < $total; $i++) {
			
		  $tmp = $_FILES['Image']['tmp_name'][$i]; // Create and ppend a variable to tmp_name
			
		  $image = $_FILES['Image']['name'][$i]; // Create and append a variable to tmp_name		  
			  
		  require('test_image.php');
	
		  //echo '<br>Image '.$i.': '.$tmp.' => '.$file;
	
		  move_uploaded_file($tmp, $file);
		  }	
		}                                           			
	} // do nothing if the first image isn't selected (meaning no image is selected)
  $_SESSION['add_provider'] = '0';
  $_SESSION['providerID'] = $providerID;
	
  //echo "<br>Provider: ".$providerID;
  //echo '<br>User: '.$id;

  require('success.php');
} 
?>

Thanks

I know this might not be what you want, but try this. Call the functions.php file within the add_provider_logic.php file.

<?php
//echo 'FILES["Image"]<pre>';
//print_r($_FILES['Image']);
//echo '</pre>';

if($_SERVER['REQUEST_METHOD'] == 'POST') {

  require('functions.php'); // Require the functions.php file

  $name = testInput($_POST['Name']);
  $email = testInput($_POST['Email']); 
  $phone1 = testInput($_POST['phone1']); 
  $phone2 = testInput($_POST['phone2']);
  $phone3 = testInput($_POST['phone3']); 
  $id = testInput($_POST['userID']);
  $method = testInput($_POST['Method']);
  $rate = testInput($_POST['Rate']);  
  $weight = testInput($_POST['Weight']); 
  $h_feet = testInput($_POST['HFeet']);
  $h_inches = testInput($_POST['HInches']);
  $waist = testInput($_POST['Waist']); 
  $cup = testInput($_POST['Cup']);
  $age = testInput($_POST['Age']); 
  $ethnicity = testInput($_POST['Ethnicity']); 
  $introduction= testInput($_POST['Introduction']);
  $thumb = testInput($_FILES['Thumb']['name']);
	
  $phone = $phone1.$phone2.$phone3;
		
  $ip = getRealIpAddr();
  


  $sql = "INSERT INTO providers
	(userID,name,email,phone,cMethod,rate,weight,hFeet,hInches,waist,cup,ethnicity,age,introduction,thumb,ip) 
	VALUES 
	(:id,:name,:email,:phone,:method,:rate,:weight,:hFeet,:hInches,:waist,:cup,:ethnicity,:age,:intro,:thumb,:ip)"; // SQL Statement
  $prepare = $conn->prepare($sql); // Prepare the SQL Statement

	// Create an array of named parameters to bind and execute
  $parameters = array(
	':id' => $id,
	':method' => $method,
	':rate' => $rate,
	':weight' => $weight,
	':name' => $name,
	':email' => $email,
	':phone' => $phone,
	':waist' => $waist,
	':hFeet' => $h_feet,
	':hInches' => $h_inches,
	':age' => $age,
	':cup' => $cup,
	':intro' => $introduction,
	':ethnicity' => $ethnicity,
	':thumb' => $thumb,
	':ip' => $ip,
  );
		
	if($prepare->execute($parameters))	{	
		
	  $providerID = $conn->lastInsertId();
  
	  $upload_directory = 'images/providers/'.$providerID;
				
	  mkdir($upload_directory);
	  mkdir($upload_directory."/del");	  
	  
	  $sql = "UPDATE users SET providerID = :providerID WHERE id = :id"; // SQL Statement
  
	  $prepare = $conn->prepare($sql); // Prepare the SQL Statement

	  // Create an array of named parameters to bind and execute
	  $parameters = array(
		':providerID' => $providerID,
		':id' => $id,
		);
		if($prepare->execute($parameters)) {
			
		  //echo "users table updated, providerID = .".$providerID." for user ".$id;
  
		  $total = count($_POST['Options']);
			   
		  for($i = 0; $i < $total; $i++) {
				   
		  $sql = "INSERT INTO providers_options (optionName, providerID) VALUES (:option,:providerID)"; // SQL Statement
			   
		  $prepare = $conn->prepare($sql); // Prepare the SQL Statement
  
		  // Create an array of named parameters to bind and execute
		  $parameters = array(
			':providerID' => $providerID,
			':option' => $_POST['Options'][$i],
		  );
		  $prepare->execute($parameters);
		  }
		}
	} else {
	  header('Location: provider_exists.php');
	  die();
	}
	//upload featured pic
			 
	if(isset($thumb)) {
		if($thumb != '') {
	

		  //echo 'Upload Diectory: '.$upload_directory;

		  $tmp = $_FILES['Thumb']['tmp_name']; // Create and ppend a variable to tmp_name
		  $image = $thumb; // Create and append a variable to tmp_name		  
		 
		  require('test_image.php');
				
		  //echo '<br>Featured Pic: '.$tmp.' => '.$file;
					
		  move_uploaded_file($tmp, $file);
		}
	}
	//upload pictures
			 
	if(isset($_FILES['Image']['name'][0])) {
		if($_FILES['Image']['name'][0] != '') {

		  $total = count($_FILES['Image']['name']);
			

		  //echo '<pre>';print_r($_FILES['Image']);echo '</pre>';
		  //echo '<br>Count of Images: '.$total.', to be uploaded!<br>';

		  // Loop through each file
		  for($i = 0; $i < $total; $i++) {
			
		  $tmp = $_FILES['Image']['tmp_name'][$i]; // Create and ppend a variable to tmp_name
			
		  $image = $_FILES['Image']['name'][$i]; // Create and append a variable to tmp_name		  
			  
		  require('test_image.php');
	
		  //echo '<br>Image '.$i.': '.$tmp.' => '.$file;
	
		  move_uploaded_file($tmp, $file);
		  }	
		}                                           			
	} // do nothing if the first image isn't selected (meaning no image is selected)
  $_SESSION['add_provider'] = '0';
  $_SESSION['providerID'] = $providerID;
	
  //echo "<br>Provider: ".$providerID;
  //echo '<br>User: '.$id;

  require('success.php');
} 
?>

I am not entirely sure why it’s acting this way when mine works without the need to put it directly within the add_provider_logic.php file.


Actually, try putting the functions.php file at the top/ outside of the if statements.

wow, the first way works, trying the other way now.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.