Jump to content

Why is my function being called twice or is it ?


JkennG

Recommended Posts

I have 2 php files and in my index file im just getting a function but its returning 2 echo's instead of just one.

 

index.php

 

 <?php

 require_once 'config_db.php';
 require_once 'database_class.php';
 $db = new database($conDB);
   echo '<br/>';
 $fetch = $db->getData();
 
?>
 
other file
 
<?php
 include('index.php');
 class database{
  function __construct($conDB)
  {
  $this->conDB = $conDB;
  }
 
  function getData()
  {
     $clientIP = $_SERVER['REMOTE_ADDR'];
     echo $clientIP;  
  $query = $this->conDB->prepare("
  INSERT INTO `ip log` (`Address`)
  VALUES ('$clientIP')
  ");
  $query->execute();
 
  }
 
 }
 
?>

 

Link to comment
Share on other sites

This looks like a circular dependency: Your index.php includes the database_class.php, but the database_class.php in turn includes the index.php. If you didn't have the require_once, you'd actually run into infinite recursion.

 

In any case, the script layout is very confusing and poor. A class file shouldn't do anything but define a class. It should definitely not run some index.php script with side effects.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.