I've been trying to get a simple login script going for a simple page I'm putting together, but I can't seem to make it work. I finally gave up and tried a ready few ready made scriots, and they don't work either.
Does anyone have any idea whats going on?
Can someone suggest what to try next.
I'm using a simple script that finds a username in the database that matches the user-entered string. It then compares the user-entered password to the db string, and responds according to if-else instructions.
Hardly rocket science.
No error message. But one pre made script lets everyone in, another keeps everyone out, and a third gives me a totally blank screen.
Any ideas?
I am a total newbie, and totally open to any suggestion. (But please don't refer me to the manual, I've checked there. I need HELP!)
Thanks so much!
You probably want to tell us what language you're using, give us some code to look at etc...
Yes buddy...you have to tell us what language you use and probably you have to learn the basics first: syntax, etc... and do the simple login script after....
Create a file that has this in it, like index.html
| Code: |
<html>
<form action="login.php" method="post">
Username: <input type="text" name="username" />
Enter your age: <input type="text" name="password" />
<input type="submit" />
</form>
</html> |
Create a file called login.php.
| Code: |
<?php
$mysql_dbname = 'riv_name';
$mysql_dbusername = 'riv_user';
$mysql_dbpassword = 'iaml33thax';
$username = $_POST['username'];
$password = $_POST['password'];
$connect = mysql_connect('localhost', $mysql_dbusername, $mysql_dbpassword);
if (!$link)
{
die('Could not connect to mySQL Database: ' . mysql_error());
}
$db_selected = mysql_select_db($mysql_dbname,$connect);
if (!$db_selected)
{
die ('Failed to select database : ' . mysql_error());
}
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($sql);
if (!$result)
{
die ('Failed to get data from table : ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
if ( $num_rows == '0' )
{
die ('Invalid Username!');
exit;
}
else
{
$row = mysql_fetch_row($result);
if ( $password == $row[1] )
{
echo ' Logged in ! ';
}
else
{
echo ' Wrong Password! ';
exit;
}
}
?> |
Remember to change these 3 values
| Code: |
$mysql_dbname = 'riv_name';
$mysql_dbusername = 'riv_user';
$mysql_dbpassword = 'iaml33thax'; |
and whatever you want after he logged in
the sql side, run this query in phpmyadmin
| Code: |
CREATE TABLE `users` (
`username` varchar(25) NOT NULL,
`password` varchar(32) NOT NULL
) TYPE=MyISAM; |
Then just make a new user through phpmyadmin manually yourself.
Thescript should work, I never tested it doh.
Why don't use the PEAR::Auth package? It seems as easy as possible. Please look below for an example.
| Code: |
<?php
require_once "Auth.php";
$dsn = "mysql://user:password@localhost/database";
$auth = new Auth("DB", $dsn);
$auth->start();
if ($auth->checkAuth()) {
// output your page for registered users
}
else {
// output your page for guests
}
?> |
For further details see Auth documentation.
tis just as simple to put $_SESSION['loggedin'] = 1; into the section where they login on the aforementioned login script. Then on any future pages put
if($_SESSION['loggedin'] != 1){die('Not logged in'); }
And ofcourse a logout script which wipes the session, obviously you could also add user types etc to the sessions..
Thanks...
I'll try that. I've tried a few similar things, but the "else" statement has varied a bit... that looks better.
And good idea about the session login whatchamacallit.
I just wanted to say thankyou, especially to noobie4life.
Turns out, I've been handling my arrays returned from the db all wrong, and it took this example to make it click for me!
Hopefully it will be easier next time, now that 1 piece of the puzzle has fallen into place. I really appreciate the patient explanations!!
I haven't the foggiest about working with sessions... Do I just use session_start when the user logs in???
I seem to be doing OK with my bits and pieces of script... but now that I'm getting closer to having them all done, I'm honestly not sure how to fit them all together. Can anyone point me to someplace helpful for that?