Back

Can't use sessions in laravel controller


PHP script to check the client's IP address:

<?php

namespace IPv4;

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

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

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

// 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] = "www.www.www.www/a";
$range [1] = "xxx.xxx.xxx.xx"/b;
$range [2] = "yyy.yyy.yyy.yyy/c";
$range [3] = "zzz.zzz.zzz.zzz/d";
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;
}

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

Application for testing its functionality:

<?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.", ";
die(var_dump($test));

?>

-> That test works well, when i change my IP (connect to VPN), the screen output switches from "1" to "0". (=not inside the ranges defined in array "$range")

But when i use the whole thing in a laravel session controller (together with smarty as a template engine), it does what it wants! sometime it shows "0", sometime "1":

<?php
namespace App\Http\Controllers;

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

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;

require_once $_SERVER ['DOCUMENT_ROOT'].'/includes/functions.inc.php';
require_once $_SERVER ['DOCUMENT_ROOT'].'/smarty/libs/Autoloader.php';

Use \Smarty;
Use \Smarty_Autoloader;

class Index extends Controller
{
	public $smarty = null;
	
    function __construct() {
		Smarty_Autoloader::register();
		$this->smarty = new Smarty;
		$this->smarty->caching = false; // prod = true
		$this->smarty->debugging = true; // prod = false
		$this->smarty->force_compile = true; // always true, incompatible with laravel
	}
	
    public function index()
    {
        require_once $_SERVER ['DOCUMENT_ROOT'] . "/testing/IPTest.php";
        
        if(!session_id() || session_id() == null || session_id() == '') {
            session_start();
        }
        
		$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		
		if(substr($url, -1) == '/') {
		    $url = substr($url, 0, -1);
		}
		
		$content = getAllFromCMSSite("site_name2='".escape_mysql(substr($url, strrpos($url, "/") + 1))."'")[0];
		$this->smarty->assign('content', $content['site_content']);
		$this->smarty->assign('title', $content['site_name']);
		
		$this->smarty->assign('pages', getAllFromCMSSite("site_type=1"));
		$this->smarty->assign('posts', getAllFromCMSSite("site_type=2", "DESC", "5"));
		
		$this->smarty->assign('ipAddressIsInRange', $_SESSION['ipAddressIsInRange2']);
		// $this->smarty->assign('ipAddressIsInRange', 77);
		
		$html = $this->smarty->fetch($_SERVER ['DOCUMENT_ROOT']."/smarty/templates/template.htm");
		$html = compress_page($html);
		
		return $html;
    }
}

Regards, Jan

jmar83 replied 10 months ago

Maybe the only is to output (echo) "0" or "1" in IPTest.php and then do a HTTP request with PHP on http://www.server.com/IPTest.php, e.g. with CURL or url_fopen or whatwever..?

Regards, Jan

jmar83 replied 10 months ago

Problem is solved.

Regards, Jan


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us