-2

I have HTML/PHP website (HTML code inside my .php)

I am using the $_GET["name"] method so that I can write the name of my guest in the link I send (for exemple : mywebsite.com/index.php?name=Mike). Here's the code I use :

<p class="guest_info"> 
        <?php echo " " . $_GET["name"]; ?> 
        <img alt="account_avatar" src="./index_files/avatar.svg">
        </p>    

Now, I would like this name to be implemented to the differents pages. My current problem is that if I give the link and after click on another page of my website, the ["name"] is not saved. I Would like to save the name used in the first given URL while navigating, and if I change the name in the link, that it automaticaly changes the pages aswell.

Thanks for helping

Jonakis50
  • 1
  • 1
  • *"I am using the $_GET["email"] method so that I can write the name of my guest in the link I send (for exemple : mywebsite.com/index.php?name=Mike). Here's the code I use :"* - So which one is it, `$_GET["email"]` or `$_GET["name"]`? In either case, the question is too broad. – Funk Forty Niner Oct 20 '20 at 18:29
  • $_GET["name"] :) – Jonakis50 Oct 20 '20 at 18:30

2 Answers2

2

You should use a PHP session or a browser cookie.

Using the session route, at the beginning of your page, be sure to add session_start().

Then you can store the value as a session value.

if(array_key_exists('name', $_GET) && !array_key_exists('name', $_SESSION)) {
    $_SESSION['name'] = $_GET['name'];
}

Now, anywhere after that code, you can use $_SESSION['name'] to access the value as long as the session persists.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
0

In order for $_GET['name'] to persist from page to page, you need to keep passing it in the URL query string. For example, something like this: <a href="index.php?name=<?php echo $_GET['name']; ?>">Link</a>

freefall
  • 319
  • 1
  • 8