Jump to content

Recommended Posts

Hi, all,

 

I have a segment of code I would like to exclude from a specific file. In this instance it would be the file located @: includes/mailhive.php

 

The code I have is here:

 

if(EMAIL_USE_PHPMAILER == 'true')
      {  
        require_once(DIR_WS_CLASSES . 'phpmailer/class.phpmailer.php');
      }

 

How can I properly exclude this function or require_once from happening if the page loaded in the browser is includes/mailhive.php? I assume PHP_SELF may work in some form or fashion, but as simple as this may be I'm bashing my head!

 

Thanks for any help!

Hi, Christian,

 

The snippet is included and necessary on ALL files with the exception of ONE, which is an add-on script that also utilizes the PHPMailer class. Because of this, the two clash. It is not so easy to go editing out the add-on I have purchased (many, many files, and using a customized version of the PHPMailer), thus the best way to go about this would be to exclude it from running when on the mailhive page. This specific line is located in an includes file applicable to each and every page, and I am just posting this one, very small snippet of the script to narrow down my issue.

 

These are some of the woes of running a heavily modified OSCommerce store, but I must make do.

 

Thanks for posting and asking, however.

I don't highly recommend it, but you could set a variable in the file you don't want it to be included in, and then in the included file check if the variable is not set.

 

the function you need is:

is_set

 

for example:

if(!is_set($dont_include)){
include();
}

Hi, MMDE,

 

Thanks for the reply. I didn't think of that, and I suppose it could work, though you're right, it seems a little "hackish" and wouldn't be module upgrade friendly.

 

Is there not a way to just perform a test of which page we are currently on and exclude it (in this instance mailhive.php)?

 

So, in brainflow terms:

 

if the page is not mailhive.php then run:

 

if(EMAIL_USE_PHPMAILER == 'true')
      {  
        require_once(DIR_WS_CLASSES . 'phpmailer/class.phpmailer.php');
      }

 

That would do the trick, and while I understand this seems novice and likely dumb, I really do appreciate the help!

Auch... OSC? Damn, that's a long time since I saw anyone running it. Bloody messy code indeed, so I understand why you're having these issues. I should know, considering how much I've worked with it in the past.

 

As a bit better alternative to MMDE's tip, there are two optional methods you can use:

  • Make sure the same file is included in both places, and only use include_once () to include it. That's what it's created for, after all.
  • Check if the class has been defined. with class_exists (), and if not then include the file.

 

I strongly recommend the first approach.

 

PS: Why aren't you at least using ZenCart? It's a fork of OSC, so the basic premise is quite similar. Only with a whole lot better code, and some actual templating system at works. Not like OSC's mixed mode & echo-galore source.

As a bit better alternative to MMDE's tip, there are two optional methods you can use:

  • Make sure the same file is included in both places, and only use include_once () to include it. That's what it's created for, after all.
  • Check if the class has been defined. with class_exists (), and if not then include the file.

 

Thanks for the tips, Christian. I will see what I can do.

 

 

PS: Why aren't you at least using ZenCart? It's a fork of OSC, so the basic premise is quite similar. Only with a whole lot better code, and some actual templating system at works. Not like OSC's mixed mode & echo-galore source.

 

At this stage I don't really have a choice. I've been running this store since 2007, and through the years have heavily modified it with even custom activation systems for digital products. To switch to a system like ZenCart would be worse than a prison sentence (I'm already in one with OSC).

 

We'll see how I go with your suggestions...any further tips with this would be great. Just so I understand, PHP_Self would NOT be a way to do this then, correct (aside from suggestions above)?

You can, but it would be an overly complex and brittle process. The two methods I mentioned above are the simplest and most robust ways of dealing with this.

 

Great! Thanks for the answer. I'm off to see what I can do now, and will let you know what I find.

 

Thanks again!

Perhaps I am thinking and hoping this is easier than it really is. :(

 

I have just created a test of this just to see what I could do by selecting two classes I know are available in both packages. Unfortunately they both appear to still be running with the following code in place:

 

if(!class_exists('phpmailerException')) {
      if(EMAIL_USE_PHPMAILER == 'true')
      {  
        require_once(DIR_WS_CLASSES . 'phpmailer/class.phpmailer.php');
      }
      }

 

I apologize for making this agonizing "dumb" to you guys, but I am attempting to learn.

 

With the above code, I get the following error still:

 

Fatal error: Cannot redeclare class phpmailerException in...

 

Thanks for the help.

 

That suggest that it's the other file that gets loaded second, and thus creating the problem. Having the complete error message, and all relevant details, would have highlighted it right away.

 

I did a horrible job explaining myself now that I read this, Christian. Let me re-state my previous code...

 

The code above was created and added to File A. I then went to File B in my browser and ended up with that error when ran, so I would assume I placed the code in the correct file, though I'm happy to be corrected since I'm not a genius here.

 

The full error is:

 

Fatal error: Cannot redeclare class phpmailerException in /home/xaviat/public_html/catalog/mailhive/common/classes/PHPMailer_5.2.1/mailbeez.class.phpmailer.php on line 2535

 

For good measure, line 2535 of the file above is the error function:

 

class phpmailerException extends Exception {
  public function errorMessage() {
    $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
    return $errorMsg;
  }

 

Based upon what you're writing, there are two options:

  • The file you edited is included before "mailbeez.class.phpmailer.php", and as such the previous check doesn't do anything.
  • File A has nothing to do with this, and as such doesn't influence this problem at all.

 

In either case, you will need to go through the code line by line, to find out where the class gets defined the first time, and thus why the problem occurs. Then you'll be better equipped to find the best way to prevent this from happening, and hopefully in the most reliable manner possible.

Went back to the drawing board. I had a feeling this didn't have to be so hard. Thanks for all the input here anyhow, but I did get it to work with the following:

 

if( $_SERVER['PHP_SELF'] != "/catalog/mailhive.php" ) {
      if(EMAIL_USE_PHPMAILER == 'true')
      {  
        require_once(DIR_WS_CLASSES . 'phpmailer/class.phpmailer.php');
      }
      }

 

Cheers!

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.