Post by supak111 » Thu Mar 28, 2024 4:54 am

I need to add some PHP code to my site but it should only execute once per visitor.

What PHP file in OC3 only runs once per visit?
PS is maybe a /system/startup.php a good place to add it?

.
.

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by straightlight » Thu Mar 28, 2024 7:35 am

I would simply create an event or use OCMod to accomplish this task by targeting a controller instead of a startup file.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by supak111 » Thu Mar 28, 2024 12:53 pm

Events seems a bit complicated, don't really understand how they work.

I can make a OCmod file however, which controller file should I be putting my PHP code in to have it execute only once?

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by nonnedelectari » Thu Mar 28, 2024 8:13 pm

supak111 wrote:
Thu Mar 28, 2024 4:54 am
I need to add some PHP code to my site but it should only execute once per visitor.

What PHP file in OC3 only runs once per visit?
PS is maybe a /system/startup.php a good place to add it?

.
.
Doesn't matter, webservers are stateless so everything is run at least once.
You should record in the session, as a session variable, whether the code has been run already or not.
Then you can make sure code is run once per session.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by paulfeakins » Thu Mar 28, 2024 11:49 pm

supak111 wrote:
Thu Mar 28, 2024 4:54 am
I need to add some PHP code to my site but it should only execute once per visitor.
You should tell us what you're trying to do, because there might be a better way.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by supak111 » Fri Mar 29, 2024 4:00 am

Ok this is what I am doing:

Right now I am using this code in my index.php to send myself android notification about what sites are referring visitors to my website.
Issue I have is that I usually get 2 even 3 notification per visitor sometimes.
Is there any way to edit this code so that it only send 1 notification per visitor/referrer?

Code: Select all

if (!empty($_SERVER['HTTP_REFERER'])) {               
    $refer = $_SERVER['HTTP_REFERER'];
}

$word1 = "google";
$word2 = "bing"

if (!empty($refer) and !strpos($refer, $word1) and !strpos($refer, $word2)) {
  
        // Target URL
    $url = "https://xdroid.net/api/message?k=k-8278286d&t=FROM&c={$refer}&u={$refer}";
      
        // Fetching headers, aka visit/ping $url above
    $headers = get_headers($url);
}

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by nonnedelectari » Fri Mar 29, 2024 7:49 am

Code: Select all

if (!empty($_SERVER['HTTP_REFERER'])) {  
	$refer = $_SERVER['HTTP_REFERER'];
}

if (!isset($this->session->data['referer_check'])) {
	$word1 = "google";
	$word2 = "bing"

	if (!empty($refer) and !strpos($refer, $word1) and !strpos($refer, $word2)) {
		// Target URL
		$url = "https://xdroid.net/api/message?k=k-8278286d&t=FROM&c={$refer}&u={$refer}";
		// Fetching headers, aka visit/ping $url above
		$headers = get_headers($url);
	}
	
	$this->session->data['referer_check'] = true;
}
mind you, most bots do not set a referer but some do and since bots do not retain a session, every single request from them is a new visit.
so on rare occasions you may still see multiple messages, nothing you can do about that unless you identify bots and exclude them from this.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by supak111 » Fri Mar 29, 2024 10:06 am

Actually yea that could be having sometimes. But I've also personally tested some of my website backlinks from forum sites and I get a double notification still here and there.

That being said, what control file should I put this PHP in if not /system/startup.php

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by nonnedelectari » Fri Mar 29, 2024 2:29 pm

Since it is only for the catalog side, I would suggest catalog/controller/startup/startup.php, anywhere would do.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by Joe1234 » Sat Mar 30, 2024 10:57 pm

I'm currently doing something where I only want something to register once per visitor, so what I did was check the current ip against the users online in catalog/controller/common/footer.php. You may have to do something extra with it though depending on your needs and time span per visit which would be either extending the time the online user is tracked, or copying the online users to a different table and checking that and setting that table to reset at an extended amount of time like 24 hours or something. I think this method is the easiest to accomplish what you need. It runs with every page load I believe, but it's what you can tap into to get what you want. There may be a more efficient way that only loads once, but I don't know that, if you find out, let me know. Also note, If you go the route of adding a different table you could also add a counter column to see which IP is constantly hitting within the allotted reset time...hence probably a bot...and block them all together.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by straightlight » Sat Mar 30, 2024 11:42 pm

supak111 wrote:
Fri Mar 29, 2024 4:00 am
Ok this is what I am doing:

Right now I am using this code in my index.php to send myself android notification about what sites are referring visitors to my website.
Issue I have is that I usually get 2 even 3 notification per visitor sometimes.
Is there any way to edit this code so that it only send 1 notification per visitor/referrer?

Code: Select all

if (!empty($_SERVER['HTTP_REFERER'])) {               
    $refer = $_SERVER['HTTP_REFERER'];
}

$word1 = "google";
$word2 = "bing"

if (!empty($refer) and !strpos($refer, $word1) and !strpos($refer, $word2)) {
  
        // Target URL
    $url = "https://xdroid.net/api/message?k=k-8278286d&t=FROM&c={$refer}&u={$refer}";
      
        // Fetching headers, aka visit/ping $url above
    $headers = get_headers($url);
}
catalog/controller/common/footer.php file already use refer. You could simply expand it to your needs by creating an extension.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by supak111 » Sun Mar 31, 2024 1:44 am

Joe1234 wrote:
Sat Mar 30, 2024 10:57 pm
I'm currently doing something where I only want something to register once per visitor, so what I did was check the current ip against the users online in catalog/controller/common/footer.php. You may have to do something extra with it though depending on your needs and time span per visit which would be either extending the time the online user is tracked, or copying the online users to a different table and checking that and setting that table to reset at an extended amount of time like 24 hours or something. I think this method is the easiest to accomplish what you need. It runs with every page load I believe, but it's what you can tap into to get what you want. There may be a more efficient way that only loads once, but I don't know that, if you find out, let me know. Also note, If you go the route of adding a different table you could also add a counter column to see which IP is constantly hitting within the allotted reset time...hence probably a bot...and block them all together.

This would be a great little extension, care to share your code?

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by Joe1234 » Sun Mar 31, 2024 12:22 pm

I had to part out a larger extension for this, and haven't tested it on its own (don't have time), but it should work as is, and easy enough to follow along. Note, I didn't implement the accessed/count code, it's just a thought that I didn't get to yet. But The column is setup in the table if you want to have a go at it.

Replace catalog/model/tool/online.php contents with this:

Code: Select all

<?php
class ModelToolOnline extends Model {
	public function addOnline($ip, $customer_id, $url, $referer) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_online` WHERE date_added < '" . date('Y-m-d H:i:s', strtotime('-1 hour')) . "'");

		$this->db->query("REPLACE INTO `" . DB_PREFIX . "customer_online` SET `ip` = '" . $this->db->escape($ip) . "', `customer_id` = '" . (int)$customer_id . "', `url` = '" . $this->db->escape($url) . "', `referer` = '" . $this->db->escape($referer) . "', `date_added` = '" . $this->db->escape(date('Y-m-d H:i:s')) . "'");


		//log the same ip address in a different table to hold it longer than one hour...this way if you use the online user report it isn't overburdened with 24 hours worth of visitors for you to comb through
		//change the 25 to however long/short you want
		$this->db->query("DELETE FROM `custom_country_ip` WHERE date_added < '" . date('Y-m-d H:i:s', strtotime('-25 hour')) . "'");

		$this->db->query("REPLACE INTO `custom_country_ip` SET `ip_address` = '" . $this->db->escape($ip) . "', `date_added` = '" . $this->db->escape(date('Y-m-d H:i:s')) . "', `accessed` = '" . Null . "'");
	
	}
	
	
	public function Custom_CheckIP($ip) {

		//check if ip was registered in the last 25 hours
		$query = $this->db->query("SELECT ip_address FROM `custom_country_ip` WHERE ip_address = '" . $ip . "'");

		if (isset($query->rows[0]['ip_address'])) {

			return $query->rows[0]['ip_address'];

		}

	}	
	
}

In catalog/controller/common/footer.php add the following before: $this->model_tool_online->addOnline($ip, $this->customer->getId(), $url, $referer);
The cookie is set so "real" visitors arent unnecessarily querying the database with every page load, and then for the bots it will check against the database for ips. Change the cookie time as needed.

Code: Select all

			if(!isset($_COOKIE["CountryIP"])) {//if the cookie is not set

				if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
					$return = $_SERVER['HTTP_CLIENT_IP'];
				} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
					$return = $_SERVER['HTTP_X_FORWARDED_FOR'];
				} else {
					$return = $_SERVER['REMOTE_ADDR'];
				}


				$ip_recently_online = $this->model_tool_online->Custom_CheckIP($return);
				$new_visit = True;

				if (isset($ip_recently_online) && $ip_recently_online == $return) {
					$new_visit = False;
				}


				if ($new_visit = True) {

					//********************************
					//Do whatever you need here.************************************************************************************************
					//********************************
						
					setcookie("CountryIP", "Cookie IP", 0 , "/", "example.com", TRUE);

				}
			
			}
Find a place to put this to install the table (or just create it manually). I put this in: admin/controller/marketplace/modification.php after: public function refresh($data = array()) {

Code: Select all

		//Create country ip table if it doesn't exist
		$this->db->query("CREATE TABLE IF NOT EXISTS `custom_country_ip` (`ip_address` VARCHAR(30), `date_added` VARCHAR(20), `accessed` INT(5), PRIMARY KEY (ip_address)) COMMENT='24hr IP list to prevent multiple checks checks.';");

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by supak111 » Sun Mar 31, 2024 9:43 pm

straightlight wrote:
Sat Mar 30, 2024 11:42 pm
catalog/controller/common/footer.php file already use refer. You could simply expand it to your needs by creating an extension.
I will look into this, might be a better idea

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by Joe1234 » Mon Apr 01, 2024 3:07 am

supak111 wrote:
Sun Mar 31, 2024 9:43 pm
I will look into this, might be a better idea
I literally gave you exactly that...which I already stated in my first post and you then requested lol. Either way, I came back to tell you disregard the whole increment count thing I was thinking about. I probably wont work, at least the way I have the query setup using "replace" in the query. It should work if you query in a different way though.
Your welcome lol.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by supak111 » Mon Apr 01, 2024 3:30 am

I meant better idea than what i was doing, not the code you sent me. Thanks btw @Joe1234

PS I really haven't had the time to look you code over. Hope you don't mind me asking question

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm

Post by paulfeakins » Tue Apr 02, 2024 7:40 pm

supak111 wrote:
Fri Mar 29, 2024 4:00 am
what sites are referring visitors to my website.
Sounds like you could use software like Matomo for this.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by supak111 » Thu Apr 04, 2024 10:18 am

paulfeakins wrote:
Tue Apr 02, 2024 7:40 pm
supak111 wrote:
Fri Mar 29, 2024 4:00 am
what sites are referring visitors to my website.
Sounds like you could use software like Matomo for this.

Never heard of them before. Will look into it for sure

.

~ OC 3.0.3.2 and OCmods only ~


User avatar
Active Member

Posts

Joined
Fri Feb 13, 2015 12:09 pm
Who is online

Users browsing this forum: No registered users and 18 guests