Search code examples
phpwordpresssessionget

PHP SESSION cant store variable from GET form variable


<?
session_start();

 $_SESSION['name'] = "$_GET["name"]";
 $_SESSION['email'] = "$_GET["email"]";
 $session_id=session_id();
 
echo"$session_id <br>  $_SESSION['name'] <br> $_SESSION['email']";

?>

I am trying to create a session to store visitor input form with GET method, I cant use POST because the form is handled by wordpress plugins and the client only gave me the GET option. The problem is:

  1. On page #1, this is the page after we submit the form, the echo is shown complete.
  2. At page #2, I already add session_start(); at top but $_SESSION['name'] and $_SESSION['email'] keep empty (change page) but $session_id is stored and show same.

What am I missing? or maybe $_SESSION is cant store $_GET?


Solution

  • So on the first page you save the values from the user input and it works as expected.

    However, when you go to the second page, your code is overriding the session with new variables, which are empty in this case. Before assigning anything to the session, you should check whether it is not empty / valid.

    For this simple code I would recommend checking via isset / array_key_exists functions.