I need to make a login thing. I got some codes from another person, but it doesn't work for me. I double and tripled check to see if my apache server and mysql were set up correctly and they were. help?

sign-in:

<html>
<head>
 
<title>无标题文档</title>
</head>
 
<body>
<form action="checklogin.php" method="post">
 
<br>
<table border="0" cellpadding="2" cellspacing="2" width="42%">
<tbody><tr> 
<td width="14%">Name</td><td width="86%">
<input style="background-color: rgb(255, 255, 160);" name="username" class="textbox" type="text"><br>
</td>
</tr>
<tr> 
<td width="14%">Password</td>
<td width="86%">
<input name="password" class="textbox" type="password">
</td>
</tr>
<tr> 
<td colspan="2">
<input name="Submit" value="Submit" type="submit">
</td>
</tr>
</tbody></table></form>
 
</body>
</html>

============================================

checklogin:

<?php 
 
session_start();
$name = trim(addslashes($_POST["username"]));
$pass = trim(addslashes($_POST["password"]));
// you may also wish to perform extra security checks here, make sure the input is valid. adding slashes would be a good start 
mysql_connect("localhost", "test_user", "password_123") or die("Couldn't Connect to Server " . mysql_error());
mysql_select_db("blog") or die("Database not found " . mysql_error());
$result = mysql_query("SELECT * FROM users WHERE username ='". $name . "' AND password='". sha1($pass) . "' ");
while ($row = mysql_fetch_array($result)){
if(mysql_num_rows($result) == 1){
print ("<h2>Login Details Verified, You May Click Below to Proceed</h2><BR>");
print ("<a href=\"blog.php\"Continue"</a>");
// You may want to assign a session variable here. This variable can then be checked on later pages that require the user to be logged in. 
$_SESSION[ legal ] =1;
} 
 
else {
Print "<h2>Invalid Username/Password</h2><BR>";
print "<a href=\"javascript:history.back(-1)\">Go Back, to try again.</a>";
 
}
}
 
 
?>

plz see watz wrong. thx

Dani AI

Generated

A blank page almost always means a PHP parse/runtime error with error display turned off — the script is failing before any HTML is emitted. While debugging add these lines at the very top of checklogin.php to see the actual error:

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

(Then reload the page and read the error; turn these off again on a production server.) PHP manual: error_reporting. (php.net)

Look for a syntax error in the posted checklogin script — the anchor/print line shown in the thread is malformed and will cause a parse error, which fits the behavior you described. Fix that line and quote session keys (using an unquoted token like $_SESSION[ legal ] is wrong; use $_SESSION['legal'] = 1;). Also guard access to $_POST with isset() so you don’t get undefined-index notices while debugging:

print('<a href="blog.php">Continue</a>');
$_SESSION['legal'] = 1;

(See the session docs for correct usage.) (php.net)

Security and maintenance notes (important if this forum thread is being read years later): do not use sha1() for passwords and avoid the old mysql_* extension. Use password_hash() / password_verify() and parameterized queries via PDO or MySQLi prepared statements instead. Example flow: fetch the stored hash with a prepared statement and call password_verify($inputPassword, $storedHash). This prevents SQL injection and uses modern password hashing practices. See PHP’s password functions and PDO docs, and OWASP guidance for password storage. password_hash / password_verify · PDO prepared statements · OWASP: Password Storage Cheat Sheet. (php.net)

Quick checklist: enable errors and read the message, fix the malformed print/HTML string, quote $_SESSION keys, isset() the $_POST fields, verify DB table & credentials (as suggested), and migrate to prepared statements/password_hash when you have the login working. As observed, the blank page almost always becomes clear once errors are visible.

Recommended Answers

All 3 Replies

You may want to check the following:

1. Do you get any error message for database connectivity?
2. Do you already have the table 'users' and all fields ready?
3. Have you stored password is sha1 encrpted? You may need to sha1($pass) as a separate line before query to database

There is not error message for the database

I have all the tables and fields in the sql.

I think the problem is that my browser can't get through the lines:
$name = trim(addslashes($_POST["username"]));
$pass = trim(addslashes($_POST["password"]));

When i put the lines there, the page just goes blank

btw, how do u make sure that it is SHA ecripted?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.