v2.5.2
Giriş yap

Session Dizisine Yeni Eleman Ekleme

coder
395 defa görüntülendi

Merhaba.

Session dizisi oluştuktan sonra bu diziye yeni bir eleman ekleme işlemini nasıl yapabilirim?

Örneğin aşağıdaki gibi bir session dizisi oluşmuş olsun. Sonra bu diziye email elemanını nasıl ekleyebiliriz?

[User] => Array
(
    [user_id] => 1
    [user_name] => Coder
)

Kodlarım

<?php
 
class Session
{
 
	private static $SessionStart = false;
 
    public static function SessionStart()
    {
        if(self::$SessionStart == false){
            session_start();
            self::$SessionStart = true;
        }
    }
 
	public static function Exists($name)
	    {
	        if(isset($_SESSION[$name])){
	            return true;
	        }else{
	            return false;
	        }
	    }
	
	public static function CreateSession($name, $data)
	    {
	        if(!self::Exists($name)){
	
	            $_SESSION[$name] = true;
	            if( is_array($data)){
	                $_SESSION[$name] = [];
	                foreach ($data as $key => $value){
	                    if($_SESSION[$name][!isset($key)]){
	                        $_SESSION[$name][$key] = $value;
	                    }else{
	                        $_SESSION[$name][$key] = $value;
	                    }
	                }
	            }else{
	                $_SESSION[$name] = $data;
	            }
	
	        }
	    }
 
}
 
----------------------------------
// Kullanım
 
Session::SessionStart();
 
Session::CreateSession("User",[
    "user_id" => 1,
    "user_name" => "Coder"
]);
 
Session::CreateSession("Merhaba","Merhaba dünya");
Cevap yaz
Cevaplar (3)
tayfunerbilen
910 gün önce

buyur dostum, örneklerle basit bir session sınıfı yazdım sana, artık nasıl kullanmak istiyorsan öyle kullan geliştir devamını, en fazla 2 boyutlu dizi oluşturabilirsin zaten session'da da daha fazlasına ihtiyacın olmayacaktır

<?php

session_start();

class Session {

    public static function get($key, $subKey = false) {
        return $_SESSION[$key][$subKey] ?? $_SESSION[$key] ?? false;
    }

    public static function create($key, $data) {
        $_SESSION[$key] = $data;
    }

    public static function append($key, $data, $singleData = false) {
        if ( !self::get($key) ) {
            throw new Exception('Ekleme yaptığın session oluşturulmamış');
        }
        if ( !is_array(self::get($key)) ) {
            throw new Exception('Ekleme yaptığın session dizi tipinde değil');
        }
        if (is_array($data)) {
            $_SESSION[$key] += $data;
        } else {
            $_SESSION[$key][$data] = $singleData;
        }
    }

    public static function prepend($key, $data) {
        if ( !self::get($key) ) {
            throw new Exception('Ekleme yaptığın session oluşturulmamış');
        }
        if ( !is_array(self::get($key)) ) {
            throw new Exception('Ekleme yaptığın session dizi tipinde değil');
        }
        if (is_array($data)) {
            $_SESSION[$key] = $data + $_SESSION[$key];
        } else {
            $_SESSION[$key]= [$data => $singleData] + $_SESSION[$key];
        }
    }

    public static function remove($key, $subKey = false) {
        if ($subKey) {
            if (is_array($subKey)) {
                array_map(function($k) use ($key) {
                    unset($_SESSION[$key][$k]);
                }, $subKey);
            } else {
                unset($_SESSION[$key][$subKey]);
            }
        } else {
            unset($_SESSION[$key]);
        }
    }

    public static function getAll() {
        return $_SESSION;
    }

    public static function dumpAll() {
        echo '<pre>';
        print_r(self::getAll());
    }

}

Session::create('user', [
    'name' => 'Tayfun'
]);

Session::dumpAll();

Session::append('user', [
    'email' => '[email protected]',
    'surname' => 'Erbilen'
]);

Session::append('user', 'anahtar', 'deger');
Session::prepend('user', [
    'id' => 1
]);

Session::dumpAll();

print_r(Session::get('user'));
echo Session::get('user', 'id');

// Session::remove('user');
// Session::remove('user', 'email');
Session::remove('user', ['email', 'anahtar', 'surname']);

Session::dumpAll();

Session::create('name', 'Tayfun');

try {
    Session::append('name', 'anahtar', 'deger'); // hatalı
} catch (Exception $e) {
    echo $e->getMessage();
}

Session::dumpAll();
coder
910 gün önce

Teşekkür ederim tayfunerbilen ve abdullahx.

abdullahx
911 gün önce
<?php

class Session
{
 
	private static $SessionStart = false;
 
    public static function sessionStart()
    {
        if(!self::$SessionStart){
            session_start();
            self::$SessionStart = true;
        }
    }
 
	public static function exist($name): bool
	    {
	        if(isset($_SESSION[$name]))
	            return true;
	        return false;
	    }
	
	public static function createSession(string $name, $data): bool
	    {
	        if(!self::exist($name)) {
	           $_SESSION[$name] = $data;
	           return true;
	        } 
	        else return false;
	    }
	    
	 public static function updateSession(string $name, string $key, $data): bool
	 {
	 	if (self::exist($name))
	 	{
	 		if (is_array($_SESSION[$name]))
	 		{
	 			$_SESSION[$name][$key] = $data;
	 			return true;
	 		} else return false;
	 	} else return false;
	 }
 
 	public static function removeSession(string $name, $multidimensional = false, $key = ''): bool
 	{
 		if (exist($name))
 		{
 			if (!$multidimensional) {
 				unset($_SESSION[$name]);
 				return true;
 			} else {
 				if (is_array($_SESSION[$name])) {
 				if (array_key_exists($key, $_SESSION[$name])) {
 					unset($_SESSION[$name][$key]);
 					} else return false;
 				} else return false;
 			}
 		} else return false;
 	}
} 

Not: Sadece iki boyutlu dizide çalışır