jmar83

With the oop way, i get a 0 value with echo (in controller), while the test app still shows the correct value:

require_once $_SERVER ['DOCUMENT_ROOT'] . "/testing/IPTest.php";
        echo \IPv4\checkIPAddressIsInRange();

No way is working: Neither session or include/require_once nor the oop one. WTF is going on, something must be completely fu***d up..

The crazy thing is that the test app works without any problems:

<?php

if(true)
{
    error_reporting(E_ALL & ~E_NOTICE);
    ini_set ( 'display_startup_errors', 1 );
    ini_set ( 'display_errors', 1 );
}

require_once $_SERVER ['DOCUMENT_ROOT'] . "/testing/IPTest.php";

if(!session_id() || session_id() == null || session_id() == '') {
    session_start();
}

$test = $_SESSION['ipAddressIsInRange2'];
echo $test.", ";
echo var_dump($test);

$test = $ipAddressIsInRange;
echo ", without session: ".$test.", ";
echo var_dump($test);

?>

...with that IPTest:

<?php

namespace IPv4;

if(true)
{
    error_reporting(E_ALL & ~E_NOTICE);
    ini_set ( 'display_startup_errors', 1 );
    ini_set ( 'display_errors', 1 );
}

if(!session_id() || session_id() == null || session_id() == '') {
    session_start();
}

require_once $_SERVER ['DOCUMENT_ROOT'] . "/testing/ipv4-subnet-calculator/src/SubnetCalculator.php";

// header ( "Content-Type: text/plain" );

$ipAddress = null;

if (! empty ( $_SERVER ['HTTP_CLIENT_IP'] )) {
    $ipAddress = $_SERVER ['HTTP_CLIENT_IP'];
} else {
    // echo "$_SERVER['HTTP_CLIENT_IP'] is empty...\r\n\r\n";
    if ($_SERVER ['REMOTE_ADDR'] != $_SERVER ['HTTP_X_FORWARDED_FOR']) {
        // echo "$_SERVER['REMOTE_ADDR'] is " . $_SERVER ['REMOTE_ADDR'] . "\r\n\r\n";
        $ipAddress = $_SERVER ['HTTP_X_FORWARDED_FOR'];
    }
}

// echo "$_SERVER['HTTP_X_FORWARDED_FOR'] is " . $ipAddress . "\r\n\r\n";

$range = null;
$range [0] = "77.239.32.0/19";
$range [1] = "46.255.168.0/21";
$range [2] = "37.35.112.0/21";
$range [3] = "185.74.112.0/22";
class SubnetCalculatorFactory {
    public static function createInstance($ipAddress, $number) {
        return new SubnetCalculator ( $ipAddress, $number );
    }
}

$subnetCalc = null;
$ipAddressAsLong = ip2long ( $ipAddress );
function checkIPAddressIsInRange() {
    global $range;
    global $ipAddressAsLong;
    for($c = 0; $c < sizeof ( $range ); $c ++) {
        $ip = null;
        $ip_match = null;
        $text = $range [$c];
        $number = substr ( $text, - 2 );
        
        if (preg_match ( '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $text, $ip_match )) {
            $ip = $ip_match [0];
        }
        
        $subnetCalc = SubnetCalculatorFactory::createInstance ( $ip, $number );
        $ipAddressRange = $subnetCalc->getIPAddressRange ();
        
        if (ip2long ( $ipAddressRange [0] ) <= $ipAddressAsLong && $ipAddressAsLong <= ip2long ( $ipAddressRange [1] )) {
            return 1;
        }
    }
    return 0;
}

$ipAddressIsInRange = checkIPAddressIsInRange();

// Use caching or not...
if(false)
{
    if(!isset($_SESSION['ipAddressIsInRange2']))
    {
        $_SESSION['ipAddressIsInRange2'] = $ipAddressIsInRange;
    }
}
else 
{
    $_SESSION['ipAddressIsInRange2'] = $ipAddressIsInRange;
}

?>

Regards, Jan

wilk_randall

I don't think you even understand much about object oriented concepts... I have never needed to use the global keyword inside of a class and I have definitely never put code like you are outside of the class when it could be used in a class method instead. You seem to be stuck in a procedural mindset. Perhaps you should watch one of these video series to catch up...

https://laracasts.com/series/object-oriented-bootcamp-in-php

https://laracasts.com/series/laravel-from-scratch-2017

jmar83

Yes, at least not a lot about the syntax in PHP... with PHP, for me it's an advantage when i don't need to use OOP... and, as far as possible, i also try to avoid functions in that language...;)

a "global" (with the include/procedural way) does also not help here so it seems:

public function index()
    {
        require_once $_SERVER ['DOCUMENT_ROOT'] . "/testing/IPTest.php";
        global $ipAddressIsInRange;
        ...
        ...
        ...

Regards, Jan

jmar83

Now i will try to use PHP "scriptlets" in Smarty template... escaped out of OO context!!

The other "solution" is to place the google analytics/statcounter.com tracking code in a site in an iframe...

The most idiotic and not elegant solutions are mostly working in IT, that's well-known to me! ;)

Regards, Jan

jmar83

In WordPress "functions.php" i use that code, zero problems:

require_once ABSPATH.'/testing/IPTest.php';
$_SESSION['ipAddressIsInRange'] = IPv4\checkIPAddressIsInRange();

..but it's not in an OO context. Same as my test application. So the problem must be OO.

wilk_randall

In WordPress "functions.php" i use that code, zero problems:

Of course you're not going to have problems; WordPress is mostly procedural. Are you unwilling to adapt to newer programming practices or something? OOP makes for much easier to maintain code, as well as reusable code.

Cronix
Cronix
1 week ago (398,210 XP)

It's ok. You're only about 20 years behind the times. Blame the modern framework for your lack of oop understanding... you might as well skip a modern framework and stick to your outdated procedural code and think you're smarter. WordPress is anything but modern/current as far as php goes. It's a relic.

jmar83

OOP != OOP, i don't hope that is too complicated to unterstand.

Java and, for example, C# is REAL OOP... C++ and Java are not, it's just wannabe... that just my humble my opinion, maybe i'm completely wrong.

In Java, there is almost no other way than to use OOP. You can't programm ALL in a static way, completely impossible..

In Java i have a lot of experience in OOD & OOP. But the syntax and the concept of PHP or C++ OOP is all very different. In Java, i have a good OO knowledge, including the "advanced" things like design patterns, null interfaces, weak and strong references, serialization, reflection, etc. pp.

"...and think you're smarter."

No, but maybe my apps and websites are faster...;)

"WordPress is anything but modern/current as far as php goes."

Now i use WordPress only as a backend, that's the reason for my Laravel-frontend-Problems... but i don't have the time to read book for hundreds of hours about PHP OOP and Laravel...

Because of that: "learning by doing"...

P.S.: Now i've solved the problem with a laravel-independent PHP script, that will be called by SJAX GET request, and it returns 0 or 1. content-type ist text/plan, really simple.

If 1 then -> ip address is in range -> do nothing... If 0 then -> ip address is not in range -> javascript block x will be executed

Thanks to everyone and best regards, Jan

Please sign in or create an account to participate in this conversation.