1

I wanted to show the variable username into another page. These are the codes I've used. This is the first page where the username is inserted.

<?php
    include('login.php'); // Includes Login Script
    if(isset($_SESSION['login_user'])){
    }
?> 

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <div id="login" align="center">
            <h2>Welcome!</h2>
            <form action="" method="post">
                <label>Username :</label>
                <input id="name" name="username" placeholder="username" type="text"><br>
                <label>Password :</label>
                <input id="password" name="password" placeholder="**********" 
                type="password">                
                <br><br>
                <input name="submit" type="submit" value=" Login ">
                <span><?php echo $error; ?></span>
            </form>
        </div>
    </body>
</html>

Then in this page I wanted to show the username that was inserted

<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
function visualizza($file) {
$f = fopen($file, "r"); // apro il file in lettura
return fread($f, filesize($file));
fclose($f);
}
?>
<html>
<main>
<div class="container">
<h2> Quiz Completato!</h2>
<p> Congratulations <?php 
$username = $_POST['username']; 
echo $username;
?>

! You completed the test</p>
<p>Final Score:<?php echo $_SESSION['score']; ?> </p>
</div>
</main>

I can't put form action="final.php", because this is the final page of a quiz, while the submit button has to send me to another page

Do you know how to do this please?

This is where the user and password are processed (login.php)

<?php


session_start(); // Starting Session
$error = ''; // Variable To Store Error Message

if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username = $_POST['username'];
$password = $_POST['password'];

// mysqli_connect() function opens a new connection to the MySQL server.
$conn = mysqli_connect("localhost", "root", "", "quizzer");

// SQL query to fetch information of registerd users and finds user match.
$query = "SELECT username, password from login where username=? AND 
password=? LIMIT 1";

// To protect MySQL injection for Security purpose
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();

if($stmt->fetch()) //fetching the contents of the row
    {
      $_SESSION['login_user'] = $username; // Initializing Session
      header("location: quizzer.php"); // Redirecting To Profile Page
    }
else {
   $error = "Username o Password sbagliate";
 }
mysqli_close($conn); // Closing Connection
}
}
?>
1
  • sessions will solve this Commented May 17, 2018 at 5:23

2 Answers 2

1

In your form element, the action attribute needs to go to another page submitting all the $_POST[] requests.

<form action="page2.php" method="post">

Now the $_POST['username'] can now be seen in the second page.

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

1 Comment

The problem with this is that if I put <form action="page2.php"> when I click the submit button it sends me to the wrong page, I need the $username in another page
1

As soon as you login u may store the username in session as follows

$_SESSION['username'] = $_POST['username'];

And echo it on any page by starting starting session

echo $_SESSION['username'];

2 Comments

Hello, sorry but I'm pretty new to this, I don't know where to put _SESSION['username'] = $_POST['username']; in the code; because it tells me undefined variable "username" in the line where i put it
Where are your username and password being processed? you didn't mention that in your question.

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.