PHP code example of oscarricardosan / session

1. Go to this page and download the library: Download oscarricardosan/session library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oscarricardosan / session example snippets


    use Oscarricardosan\_Session_\_Session_;
        
    class _MSession_ extends _Session_
    {
        
        /**
         * @param bool $if_fails_close_session
         * @return bool
         */
        public static function verificateUserSession($if_fails_close_session= true)
        {
            $token_user = self::getAttr('user_id');
            $user_data= // Code-sql that the user obtains from the session data.
            if (password_verify($_POST['password'], $hash)) {
                self::setUserData($user_data);
            }else{
                self::logout('/');
            }
        }
            
    }
    

    //In normal php

    session_start();
    $_SESSION["newsession"]=$value;
    $_SESSION["name"]= 'Juan';
    
    //With _Session_
    _MSession_::setAttr('newsession', $value);
    _MSession_::setAttr('name', 'Juan');

    //Normally

    session_start();
    if(isset($_SESSION["name"]))
        echo $_SESSION["name"];
    else 
        echo 'Sin nombre';
        
    $products= isset($_SESSION["products"]) && is_array($_SESSION["products"])?$_SESSION["products"]:[];
    

    //With _Session_
    echo _MSession_::getAttr('name', 'Sin nombre');
    
    $products= _MSession_::getAttr('products', []);
    


    _MSession_::verificateUserSession();
    
    print_r(_MSession_::userData());//Al user data 
    
    echo "Hola "._MSession_::userData()['name']; // Attribute in user data
    


index_product.php

<form action="store_product.php">
    <input type="submit" value="Send">   
</form>

<div> Errores:<div> 
<ul style="color:darkgreen;">

     foreach(_MSession_::getFlashErrors as $error){
         echo "<li>$error</li>";
     }   


    //Close session and redirect to specific page.
    _MSession_::logout('page_to_redirect.php');
    
    //Clears the session but does not delete the flash variables
    _MSession_::destroy(false);
    
    //Destroy the session and delete the flash variables
    _MSession_::destroy(true);