PDA

View Full Version : register works how would i do a login?



Templarian
September 8th, 2005, 07:35 PM
Below are the files that omega gave me to do registering.

Now that I understand how to do the register to database, I want to know how I would make a login?

I know its probably by sending the username and pass to the .php file and having it somehow load from the database and checking each one with the username and pass that was sent.

//edit, okay if someone know the code in php to check it or has it from another project that will work fine too. i think i can figure out how to get variables from a .php with flash.

Thanks feel free to IM me.

SlowRoasted
September 8th, 2005, 09:16 PM
take the username and password from a login form and check to make sure its in the database. If it is in the database, do whatever you want, start a session saying they are logged in. If the username and password is not there then send them to an error page. You can then track the session on each page and have different levels of access, etc.

Templarian
September 9th, 2005, 06:26 AM
oh god... do u have an example or a tutorial showing this? i'm a newb at php basically all i can do is save data to a database and i cant find any tutorials that show how to take it from the database and place it in the .php to be checked.

so far i have something like this but i kinda dont know what i'm suppose to put in the middle to take the variables from the database.
the fields in it are id,user,pass,aim


$query="SELECT * FROM contacts";
$result=mysql_query($query);
//i think this tells me how many people have registered.
$num=mysql_num_rows($result);
$i = 0;
while ($i < $num) {

//what do i put here i have no idea at all to get the vars off the database to be checked.

$i++;
}

i hate just asking for code, but im horribly stuck at this part and cant figure it out.

//edit i think i found a tutorial on kirupa, dont know how i didnt see it before kinda funny it only shows how to do logins and i already have register. i'll post when i'm done under source and experiments probably if i get it to work.

SlowRoasted
September 9th, 2005, 01:13 PM
I learned how to do it from this book;)

http://www.amazon.com/exec/obidos/tg/detail/-/0321186486/qid=1126286401/sr=8-2/ref=pd_bbs_2/104-4374333-5929560?v=glance&s=books&n=507846

andres
September 9th, 2005, 04:34 PM
This should work:



//get user info from log in form
$Username = $_POST['username'];
$Password = $_POST['password'];

//connect to the database server and handle errors
$db = @mysql_connect($theserver, $theuser, $thepassword);
if (!$db) {
header("Location: index.php?err=db");
exit();
}
//connect to database and handle errors
mysql_select_db($thedb,$db);
if (! @mysql_select_db($thedb) ) {
header("Location: index.php?err=db");
exit();
}


//Select user info from DB
$sql = "SELECT * FROM your_table WHERE username=$Username";
$result = mysql_query($sql);
//if user exists in database
if ($myrow = mysql_fetch_array($result)) {
//if password incorrect for this user
$Pass = $myrow['password'];
if ($Pass != $Password) {
header("Location: index.php?err=pass");
exit();
} else {
//if password is right
header("Location: logged.php");
exit;
}
} else {
//if user doesn't exist
header("Location: index.php?err=user");
}


Here I redirect the user to the a page (index.php) everytime there is an error, using a variable to display an error message for each case.
And if there are no errors the user is redirected to logged.php
Hope this helps.

cholin
September 9th, 2005, 06:13 PM
<?php
$username = addslashes($_POST['username']);
$password = md5($_POST['password']);
$result = mysql_query("SELECT * FROM contacts WHERE username='$username' AND password='$password'");
if (mysql_num_rows($result) < 1) {
// ERROR
} else {
// THE USER IS REGISTERRED, DO WHAT YOU WANT HERE
}
?>

Thats the simplest way to just demonstrate, otherwise, to send **** back through flash, use something like this:


<?php
$username = addslashes($_POST['username']);
$password = md5($_POST['password']);
$result = mysql_query("SELECT * FROM contacts WHERE username='$username' AND password='$password'");
if ($row = mysql_fetch_assoc($result)) {
echo "username=$row['username']&name=$row['name']&address=$row['address']";
exit();
}
echo "error=bad_information";
?>

ETC. By echo'ing out the information, it will be returned to flash to be read into a loadvar. This way, you can do:


var loadVar = new loadVars;
loadVar.username = ______;
loadVar.password = ______;
loadVar.sendAndLoad(...........);

Templarian
September 9th, 2005, 09:27 PM
//deleted

//edit, wow all i had to do was change checklog to _root.checklog

yea. thx guys for ur help.