Skip to content
Advertisement

How to load users name from session ID, PHP

How do I load a user’s name from his session ID?

So it should work like this: User goes on the website for the first time (gets his session id) enters his name and from now on gets redirected to the website that shows his name.

I have worked out the redirecting stuff with the following code:

<?php 
session_start();

if (!isset($_SESSION['visited'])) {
   echo "Enter your name please";
   $_SESSION['visited'] = true;
} else {
   header('Location: /userpage');
}
?>

Advertisement

Answer

Create a $_SESSION that has the name in it with a POST or GET

HTML

<form type='GET'>
    <input type='text' name='visitor'>
    <input type='submit' name='submit_visitor' value='click'>
</form>

PHP

if(isset($_GET['submit_visitor']))
{
  $visitor = $_GET['visitor'];
  $name = $_SESSION['name'];
}
else
{
   $name = "";
}

echo $name;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement