/    Sign up×
Community /Pin to ProfileBookmark

hi! I’m new to PHP. I got the script somewhere but when I tried to log in, it kept showing — Incorrect Username or Password —
what’s wrong with the script? I appreciate

michael

index.php

[code]
<?
// Use session variable on this page. This function must put on the top of page.
session_start();

////// Logout Section. Delete all session variable.
session_destroy();

$message=””;

////// Login Section.
$Login=$_POST[‘Login’];
if($Login){ // If clicked on Login button.
$username=$_POST[‘user_name’];
$md5_password=md5($_POST[‘user_pass’]); // Encrypt password with md5() function.

// Connect database.
$host=”localhost”; // Host name.
$db_user=””; // MySQL username.
$db_password=””; // MySQL password.
$database=””; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Check matching of username and password.
$result=mysql_query(“select * from tablename where user_name=’$user_name’ and user_pass=’$md5_password'”) or die(mysql_error());
if(mysql_num_rows($result)!=’0′) { // If match.
session_register(“user_name”); // Craete session username.
header(“location:main.php”); // Re-direct to main.php
exit;
}else{ // If not match.
$message=”— Incorrect Username or Password —“;
}

} // End Login authorize check.

mysql_close($connect);
?>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

<? echo $message; ?>
<form id=”form1″ name=”form1″ method=”post” action=”<? echo $PHP_SELF; ?>”>
<table>
<tr>
<td>User : </td>
<td><input name=”user_name” type=”text” id=”user_name” /></td>
</tr>
<tr>
<td>Password : </td>
<td><input name=”user_pass” type=”password” id=”user_pass” /></td>
</tr>
</table>
<input name=”Login” type=”submit” id=”Login” value=”Login” />
</form>

[/code]

page.php

[code]

<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
if(!session_is_registered(“user_name”)){ // if session variable “username” does not exist.
header(“location:index.php”); // Re-direct to index.php
}
?>

<p>Hello <? echo $_SESSION[‘user_name’]; ?>! You are now Logged in.</p>
<p><a href=”index.php”>Logout</a></p>
[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Michaelttkk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...