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

Asked

Viewed 529 times

0

It is as follows, I was asked to do this: Make the user can pass the parameter in the image url and the desired color (to change the image and color according to the client), at the time you receive this color and image, I must save in session (Session) per client so that we don’t lose the image and color reference when browsing the window.

  • $_SESSION['cor'] = $_GET['cor']; ?

  • Check if for example $_GET['cor'] exists if make the assignment in $_SESSION

  • session_start(); if (isset($_GET['color'], )$_GET['image']) { //a check like this? and to make the assignment?

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 her:

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

Destroying a session:

session_destroy();

Delete a specific variable:

unset($_SESSION['cor']);

Example for Code Igniter (user request)

//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:

Documentación Oficial
Translated Documentation

  • Oops, thanks! You know how I could do using that Codeigneter Session? " $this->Session->"

  • @Guilhermewermann Done, I don’t use code ignitor... but I believe that’s it there, or you can pass an array as well. See: http://www.plasmadesign.com.br/codeigniter/user_guide-pt_BR/libraries/sessions.html

  • Thanks bro, thank you so much! I’m starting on the schedule now and still have these difficulties. It helped a lot!

  • @Guilhermewermann...

  • @Marcelodiniz thank you, I will include the official in the references.

  • Rafael, would that be correct? session_start(); if(isset($_GET['color'], $_GET['image'])) { $color = $_GET['color']; $image = $_GET['image']; $this->Session->set_userdata('color', $color); $this->Session->set_userdata('image', $image); }

  • You can use it like this $imagem = isset($_GET['image']) ? $_GET['image'] : 'imagem_padrao'; $this->session->set_userdata('image', $imagem); It’s there in the example... Don’t forget to handle the received parameters.

  • But just for me to understand, it would be right too?

  • @Guilhermewermann I believe so... :)

  • I’m sorry, I don’t understand the treatment part of the parameters received...

  • @Guilhermewermann ah sorry, I forgot they don’t go to the bank... so don’t worry about it.

  • Ah ok! Thank you very much, I’m programming less than a month and really helped me a lot!

  • @Guilhermewermann feel free... whenever you have questions related to programming put here in the stack :)

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.