/    Sign up×
Community /Pin to ProfileBookmark

dynamic login

I don’t think my code was correct – but i still added the

[code=php]..[/code]

to make it clearer.

Simply, I am looking for a way to display unsuccesful message on the same page where user tries to login (if no match found in the database). similarly, a message to say login successful and load the next page.

Dear all,

I am writing php code to connect to mysql server, check the user login and password entered in a form with the details in the table. if the logins exist I would like the user to be directed to the next page. alternatively the user should remain on the original page and an error message is displayed.

my code is not working. I enter wrong and correct logins and remain on the same page – nothing happens.

the form in the original page:
<form name=”loginform” action=”userlogin.php” method=post>

the userlogin.php contains:

[code=php] <?php

if ((!$_POST[username]) || (!$_POST[password])){
header(“location: homepage.php”);
exit;
}

$conn = mysql_connect(“localhost”, “root”, “”)
or die(mysql_error());
mysql_select_db(mysql, $conn) or die(mysql_error());

$sql = “select em_name from employee WHERE em_login = ‘$POST[username]’ AND em_password = password(‘$_POST[password]’)”;

$result = mysql_query($sql,$conn) or die(mysql_error());

if (mysql_num_rows($result) == 1) {

$name = mysql_result($result, 0, ’em_name’);

setcookie(“auth”, “1”, 0, “/”, “localhost”, 0);

$display_block = “<p>hello $em_name!</p>”;

}

else {

//redirect back to home page if login not found

header(“location: homepage.php”);
exit;
}
?>

<HTML>
<Title>User login</title>
</head>
<body>

<? echo “$msg”; ?>
</BODY>
</HTML>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 31.2006 — Please start by wrapping the code examples in your post with [b][ php ] ... [ /php ][/b] tags (without the spaces I used here). This will make your code much easier for us to read, and then hopefully to help you. (Click the "Edit" button at the bottom right of your post.)
Copy linkTweet thisAlerts:
@Doc_ThirstJul 31.2006 — I use this, not the most fool proof, but its an internal site:

[CODE]<?php
echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' href='style.css' type='text/css' media='screen'>";
echo "</head>";
echo "<body>";
$handle = mssql_connect ("xxxx", "xxxx", "xxxx");
mssql_select_db("xxxx");

$username = trim($_POST['username']);
$password = trim($_POST['password']);

if (isset($username) && isset($password))
{
$recordset = mssql_query ("SELECT * FROM xxx WHERE (username = '$username') AND (password = '$password')", $handle);
$lim = mssql_num_rows($recordset);
if ($lim == 1)
{
echo "<B>Logon Successful. Loading page...</B>";
echo "<meta http-equiv='refresh' content='2;url=menu.php'>";
}
else
{
echo "<B>Invalid User ID or Password. Please Try again...</B>";
echo "<meta http-equiv='refresh' content='2;url=login.php'>";
}
else
{
echo "<B>User ID or Password left blank. Please Try again...</B>";
echo "<meta http-equiv='refresh' content='2;url=login.php'>";
}
?>
</BODY>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@memeauthorJul 31.2006 — thanks doc thirst. where do i place this code to test if it works? on the same page where the login form is placed?
Copy linkTweet thisAlerts:
@memeauthorAug 13.2006 — Hi there,

I've used the code above and it works fine. I have the user logs in in the home page, then i have a seperate page called user_login where i placed the code above from doc thirst to check the login and display the relevant message and direct the user to the right page. on successful login the user is returned back to the home page.

I tried to create a session for the successful login to store the id of the user so that from that point forward the user will be identified and their details can be used for payment etc. I don't think this is working.

Here is what I did:

1) I inserted the following at the top of the userlogin page:

[code=php]<?php
$page_name='userlogin';
session_start();
?>
<html>
</body>

<?php

echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' href='style.css' type='text/css'>";
echo "</head>";
.
.[/code]


2) then i inserted lines on the same userlogin page to retrieve the user id and name as follows:

[code=php]<html>
</body>

<?php

echo "<html>";
echo "<head>";
echo "<link rel='stylesheet' href='style.css' type='text/css' media='screen'>";
echo "</head>";
echo "<body>";
$handle = mysql_connect ("localhost", "meme", "");
mysql_select_db("mysql");

$username = trim($_POST['username']);
$password = trim($_POST['password']);

if (isset($username) && isset($password))
{
$recordset = mysql_query ("SELECT user_id, name FROM user WHERE (login = '$username') AND (password = '$password')", $handle);
$lim = mysql_num_rows($recordset);
if ($lim == 1)
{
echo "<B>Logon Successful. Loading page...</B>";
echo "<meta http-equiv='refresh' content='2;url=homepage.php'>";

echo "<p><SPAN class="ht">Hello, <b>".$recordset["name"]."!</b></SPAN>";

//CREATE A SESSION VARIABLE TO STORE THE ID OF THE USER RETRIEVED
$_SESSION[sess_userid]=$lim["user_id"];

//CREATE A SESSION VARIABLE TO STORE THE NAME OF THE USER RETRIEVED
$_SESSION[sess_name]=$lim["name"][0];


//DISPLAY THE user ID RETRIEVED FROM THE user TABLE
//echo "<p><SPAN class="result2">your user id is: <b>".$lim["user_id"]."</b></SPAN>";
}
else
{
echo "<B>Invalid User ID or Password. Please Try again...</B>";
echo "<meta http-equiv='refresh' content='2;url=homepage.php'>";
}
}

else
{
echo "<B>User ID or Password left blank. Please Try again...</B>";
echo "<meta http-equiv='refresh' content='2;url=homepage.php'>";
}

?>
</BODY>
</HTML>[/code]


when i try a correct login this is what I get:

Logon Successful. Loading page...

Hello, !

the user name is not displayed and their details are not posted to the home page where they are directed to.

Please let me know what I am doing wrong.

Kind Regards,
×

Success!

Help @meme 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 4.24,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...