简体   繁体   中英

I am getting a blank screen on my pdo login

here's some code i did. I am trying to login to a certain page if my username/password are both correct - but I keep on getting a blank white page even if the username/password are either CORRECT or WRONG! I have 3 main files that I did. Home.php, enter.php, and connection.php.

Here's the code for Home.php:

    <?php
session_start();
?>

<html>
<h1 align="center">Welcome To Home Page</h1>
<form method="POST">
<table border="0" align="center">

<tr>

<td>

<?php
echo $_SESSION["firstname"]; 
if($_SESSION["firstname"]==null)
{
?>
    <a href="enter.php">Login</a>
    <a href="registration.php">Registration</a>
    <a href="enter.php">Logout</a>
<?php
}
else
{
?>
<a href="enter.php">Logout</a>
<?php
}
?>
</td>

</tr>

<tr>
<td>
<img src="pix.jpg" width="500" length="500"/>
</td>

</table>
</form>
</html>

Here's the code for Enter.php:

    <html>

    <form action="connection.php" method="POST">
                        <ul>
                            <li>
                                Email: 
                                <input type="text" maxlength="30" name="emails" />
                            </li>

                            <li>
                                Password : 
                                <input type="password" maxlength="30" name="passwords" />
                            </li>
                            <input type="submit" maxlength="30" name="logins" value="Login" />

                        </ul>
                    </form>
    </html>

Here's the code for Connection.php:

    <?php
function getConnection(){
$db =  new PDO("mysql:host=localhost;dbname=demodatabase", "root","********");
$db ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPION);
return $db;

$db = getConnection();
$stmt = $db->prepare("
    SELECT * FROM subscriber WHERE Email_id = :emails AND Password = :passwords
");
$stmt->bindParam(":emails"   , $users    );
$stmt->bindParam(":passwords", $passwords);
$stmt->execute();
$total = $db->rowCount();
$count = $stmt->rowCount(); 
    if ($count > 0) { 
    session_start(); 
    $_SESSION['firstname'] = 'yes'; 
    header('location: home.php'); 
    exit; 
    } 
}
?>

So why do I keep getting blank pages?

Look at your code:

    <?php
function getConnection(){
    // …
}
?>

You have everything in one function and never call that. You need a closing curly brace here:

function getConnection(){
    $db =  new PDO("mysql:host=localhost;dbname=demodatabase", "root","********");
    $db ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPION);
    return $db;
}  // here

$db = getConnection();

Properly indenting your code would have helped.

On a side note, it's clear that you are storing passwords in plain text. Never do that! Use a good password hashing algorithm to hash passwords!

Also, using session_start or header after something has been sent to the browser is useless. Watch the unwanted spaces before <?php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM