0

I am trying to debug my php/html code for a faux login project. The basic idea is you can log in with one of two different submit buttons (so no validation) and then be directed to a page that recognizes who is logged in, using sessions. Right now I am just trying to find out why my code isn't working, before I start styling it in anyway.

Here is my first page:

< ?php
session_start();

$_SESSION["username1"] = "Bob";
$_SESSION["username2"] = "Jo";

? >

<! DOCTYPE html>
<html>
<head>

   <title>Login </title>

</head>

<body>

   <h1>Login</h1><br>
   <br>
   <form action="fauxLogin.php" method="post">
      <input type="submit" name="username1" id="Bob" value="Login as Bob"><br>
      <input type="submit" name="username2" id="Jo" value="Login as Jo"><br>
   </form>

</body>
</html>

And here is the code for my second page:

<?php

session_start();

if(isset($_POST['username1'])){
    "Welcome " . $_SESSION['username1'] . "! You are now logged in.";
}
 elseif(isset($_POST['username2'])){
   "Welcome " . $_SESSION['username2'] . "! You are now logged in.";
 }


? >

When I click to login, I am directed to just a blank page. Am I not correctly using the isset function? Or session? Or accessing the forms? Do I need to close the session before it will work?

Thanks!

3 Answers 3

4

Try echoing your output:

"Welcome " . $_SESSION['username1'] . "! You are now logged in.";

Should be

echo "Welcome " . $_SESSION['username1'] . "! You are now logged in.";

Otherwise, PHP doesn't do anything, it just concatenates your string, and then it never outputs it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was it. Glad it wasn't a bigger problem! Thank you!
3

Echo out the content. For example:

echo "Welcome " . $_SESSION['username1'] . "! You are now logged in.";

1 Comment

Yes, that was it. Glad it wasn't a bigger problem! Thank you!
0

Your are not printing anything in the page that's why the page is empty . Use echo to print your message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.