How to save data passed by parameter in the url in a session?

0

This is the following, I was asked to do this: Make the user to pass in the url the image and the desired color (to change the image and color according to the client), the moment you receive this color and image, I must record in session (session) per client so that we do not lose the reference of image and color when we navigate in the window.

    
asked by anonymous 19.03.2015 / 14:38

1 answer

3

You can use the following way:

//Iniciamos a sessão
session_start("customizacao");
//Armazenamos seus valores
$_SESSION['imagem'] = isset($_GET['variavel_imagem']) ? filter_input(INPUT_GET, 'variavel_imagem') : 'imagem_padrao';
$_SESSION['cor'] = isset($_GET['variavel_cor']) ? filter_input(INPUT_GET, 'variavel_cor') : 'azul';

Use it:

echo $_SESSION['cor']; ou $cor = $_SESSION['cor'];

Destroying a session:

session_destroy();

Delete a specific variable:

unset($_SESSION['cor']);

Example for Code Igniter

//Ou utilize a forma proposta pelo code igniter para obter valores
$cor = isset($_GET['variavel_cor']) ? filter_input(INPUT_GET, 'variavel_cor') : 'azul';
$imagem = isset($_GET['variavel_imagem']) ? filter_input(INPUT_GET, 'variavel_imagem') : 'imagem_padrao';
$this->session->set_userdata('cor', $cor);
$this->session->set_userdata('imagem', $imagem);

References:

Official Documentation
Documentation Translated

    
19.03.2015 / 14:42