The following is the PHP code to my login script:
<?php
session_start();
if(isset($_POST['submitted']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$connect = mysqli_connect("localhost", "root", "root", "test");
if (mysqli_connect_errno()){
echo mysqli_connect_error();
}
$query = "SELECT * FROM members where username = '$username'";
$result = mysqli_query($connect, $query);
$log = mysqli_fetch_array($result);
$dbusername=$log['username'];
$dbpassword=$log['password'];
//echo "<h1> ". $log['username']." ".$log['password']."</h1>";
if($username == $dbusername && $password == $dbpassword)
{
$_SESSION['username'] = $dbusername;
$_SESSION['password'] = $dbpassword;
header("Location: Correct.html {$_POST["redirect"]}");
}else
{
header("Location: Incorrect.html {$_POST["redirect"]}");
}
} else {
echo "boo";
}
?>
As you can see, the user is redirected to the 'Correct.html' page should they enter an existing username and password combination. The problem is passing information onto the 'Correct.html' page. I would like the 'username' variable to be passed-on so that the page will show a "Welcome 'username'" message. Now from all my attempts I am aware that I need the following piece of code on the 'Correct.html' page for this variable to be successfully passed through:
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Your session is running " . $_SESSION['username'];
}
?>
I have also found out that for this to work, I need to change the extension of my page from .HTML to .PHP. Now the problem is that if I change the extension, my 'Correct.php' page will no longer load, and I am shown a blank screen. I have tried online HTML to PHP converters (which seem to echo every line of my code), but this still does not work.
Would anyone be able to help me on one of two things:
- Provide me a solution to passing through a PHP variable in a HTML script (easier, but not sure if possible?)
- Correctly convert my HTML code to PHP (longer, but would ensure that variables can easily be passed through)
Please bear in mind that I already have a MySQL database and table setup (you probably already figured that from the code).
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation to accomplish this because you will probably create severe SQL injection bugs. NEVER use string interpolation with$_POSTparameters."{$_POST["redirect"]}"?