I am currently creating a website that will allow the user to log in using only the username, no password is required. As soon as the user typed his name in the form, their name should then be placed on all the pages that they visit, until they log out.
The / s problem I am facing is that the username does not appear on other pages after logging in. Instead, I'm having problems like errors (Note: Undefined index: username in / ceri / homes 1 / s / sec17 / public_html / cs25010 / home.php on line 41) and nothing is displayed at all.
Here is the code for the login page:
<?php session_save_path("/aber/sec17/public_html/cs25010/tmp"); session_start(); if (empty($_SESSION['username'])) { if (isset($_POST['submit'])) { $_SESSION["username"] = $_POST["username"]; header("Location: home.php"); } } ?> <!DOCTYPE html> <html> <head> <title>Sean Coyne Food Shop</title> <link href="style.css" type="text/css" rel="stylesheet"/> <link rel="icon" type="image/x-icon" href="images/favicon.ico" /> <meta name="description" content="Welcome to Sean Coyne Food Shop" /> </head> <body> <div id="page"> <div id="logo"> <img src="images/logo.jpg" alt="Sean Coyne Food Shop" title="Sean Coyne Food Shop" width="400px" height="70px"/> </div> <div id="nav"> <div id="menu"> <ul> <li><a href="home.php">Home</a></li> <li><a href="database.php">Products</a></li> <li><a href="drink.php.html">Offers</a></li> <li><a href="about.php">About Us</a></li> <li><a href="findus.php">Where to find us</a></li> <li><a href="contact.php">Contact</a></li> </ul> </div> </div> <div id="main"> <h1>Welcome to Sean Coyne Food Shop</h1> <h2>Please Log In below:</h2> <br></br> <div id="login"> <?php echo '<form action="home.php" method"post"> <input type="text" name="username" text="input username" placeholder="Username" required> <input type="submit" name="submit" value="submit" /> </form>'; ?> </div> </div> </div> </body> </html>
And here is the code for the main page: (I wonβt post the username here when itβs finished, it's just while I test it to see if it works)
<?php session_start(); ?> <!DOCTYPE html> <html> <head> <title>Home Page</title> <link href="style.css" type="text/css" rel="stylesheet"/> <link rel="icon" type="image/x-icon" href="images/favicon.ico" /> <meta name="description" content="Welcome to Sean Coyne Food Shop" /> </head> <body> <div id="page"> <div id="logo"> <img src="images/logo.jpg" alt="Sean Coyne Food Shop" title="Sean Coyne Food Shop" width="400px" height="70px"/> </div> <div id="nav"> <div id="menu"> <ul> <li><a href="index.php">Home</a></li> <li><a href="database.php">Products</a></li> <li><a href="drink.php.html">Offers</a></li> <li><a href="about.php">About Us</a></li> <li><a href="findus.php">Where to find us</a></li> <li><a href="contact.php">Contact</a></li> </ul> </div> </div> <div id="main"> <h1>Welcome to Sean Coyne Food Shop</h1> <?php echo $_SESSION['username']; ?> </div> </div> </body> </html>
Here's what it looks like when you log in:

user3211033
source share