v2.5.2
Giriş yap

Session Dizisine Yeni Eleman Ekleme

coder
410 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");
tayfunerbilen
942 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();