Old 05-25-2005   #1 (permalink)
Registered User
 
Space Cowboy's Avatar
 
Join Date: May 2005
Location: England (South)
Posts: 40

PHP Login Help

Hay im new here but i looking for some help with This Tutorial on this site. I've got it all working and running fine and its great. But i've got a slight problem, im using the php along with flash to make a multiplayer game. I've got a login that works fine but i was trying to add in the php that only alows you to login if you have activated you're account.

Current Login.php
Code:
<? 

//Database Connectivity Variables//

$DBhost = "localhost";
$DBuser = "";
$DBpass = "";

$db_name = "";
$login_table = "Users"; 


//Create a connection to the MySQL Database//

$connection = @mysql_connect($DBhost,$DBuser,$DBpass) or die("Couldn't Connect.");
$db = @mysql_select_db($db_name, $connection) or die("Sorry, I could not select the requested Database ");
$password = md5($pass);
//SQL Querys
$sql = "SELECT * FROM $login_table WHERE username = \"$username\" and password = \"$password\"
";

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");



/*Now checking for the results of the query. if the results are not equal to zero then everything ok, anything else then the authentication failed*/

$num = mysql_num_rows($result); 

if ($num != 0) { 

   
// Send Results Back To Flash //  

echo "status=Authentication Accepted"; 
echo "&chklogin=good";
echo "username=$username";
exit;  
}
else {
// Send Errors Back To Flash //
 echo "chklogin=bad";
 echo "&status=Athentication Failed";  
}

?>
This currently works fine, but i want to get this code added insome how so that the user has to activate account before logging in!


Code:
 <?php
session_start();
include 'config.php';

if(isset($_POST['login']))
{

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

$query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error());

$row = mysql_fetch_array($query);

// now we check if they are activated

if(mysql_num_rows($query) > 0)
{

if($row['Activated'] > 0)
{

$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name'];

header("Location: member.php");

} else {

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">

</head>

<body>
<div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div>
</body>
</html>
';

}

} else {

echo '
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p>
<p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>

</div>
</body>
</html>
';

}

} else {

?>
<html>
<head>
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="wrapper">

<div id="head">the login page</div><br>
<div id="main">
<p>You must login to view this page. Enter your username and password below and hit submit:</p>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<p>Username:<br>
<input name="username" type="text" Cid="username">

<p>Password:<br>
<input name="password" type="password" id="password">
</p>
<p>
<input name="login" type="submit" id="login" value="Submit">
</p>
</form>
<p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p>
<p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p>
</div>

</div>

</body>
</html>
<? } mysql_close($l); ?>

As you can probably guess, this script utilizes PHP SESSIONS, so a session is started with the session_start() command, and then config.php is included as before. We then check whether the user has submitted the form or not.  If they have, it is processed, login details are checked, and the session variables are set to confirm that he/she is logged in. If this is not the case, the user is presented with a nice form instead.  Nice and simple!

Activation Script (activate.php)
Not many membership systems include activation scripts, but I wanted to show you how to make yours “unspammable” and this is the easiest way. To put it simply, the user must enter a valid e-mail address otherwise he/she will not be able to validate their account and will not be able to login! Its a nice way to ensure that all your e-mail addresses are valid. Here’s the code you need:

<?php

include 'config.php';

$id = $_GET['id'];

$query = mysql_query("SELECT * FROM Users WHERE Actkey = '$id' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($query);

if(mysql_num_rows($query) > 0){

$user = $row['id'];

$do = mysql_query("UPDATE Users SET Activated = 1 WHERE id = '$user' LIMIT 1") or die(mysql_error());
$send = mail($row['Email'] , "Activation Confirmation" , "Thank you for activating your account, you are now fully registered and able to use our services.\n\nTo login, click the link below:\nhttp://CHANGETHISURL.COM/login.php" , "FROM: auto@mailer.com");

if(($do)&&($send))
{

echo '<link href="style.css" rel="stylesheet" type="text/css">
<div id="success">
<p>Activation successful! A confirmation email has been dispatched. You can now login!</p>
<p><a href="login.php">Click here</a> to goto the login page.</p>
</div>';

} else {

echo '<link href="style.css" rel="stylesheet" type="text/css">
<div id="error">
<p>We are sorry, there appears to be an error processing your activation. Please try again later.</p>
</div>';

}

} else {

echo '<link href="style.css" rel="stylesheet" type="text/css">
<div id="error">
<p>Sorry, your activation code was incorrect. Please try again.</p>
</div>';

}

mysql_close($l);
?>
Can some one help me out here thanks!
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-25-2005   #2 (permalink)
Invicible Snake
 
ParaSnake's Avatar
 
Join Date: Sep 2004
Location: Ho Chi Minh City,Vietnam
Posts: 668

Send a message via MSN to ParaSnake Send a message via Yahoo to ParaSnake
hey,i saw u did that,whats ur problem,i dont get it.
__________________
ParaSnake is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-25-2005   #3 (permalink)
Registered User
 
Space Cowboy's Avatar
 
Join Date: May 2005
Location: England (South)
Posts: 40

I want to make
Code:
if($row['Activated'] > 0)
{
This work with the first php script in the above post! So that the account has to be activated before the login works!
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-26-2005   #4 (permalink)
Invicible Snake
 
ParaSnake's Avatar
 
Join Date: Sep 2004
Location: Ho Chi Minh City,Vietnam
Posts: 668

Send a message via MSN to ParaSnake Send a message via Yahoo to ParaSnake
set the default value of 'Activated' in ur database is 0 by using phpmyAdmin or sth.
after user has registered on ur site,use the mail function to send them an email with sth that mysql can figure out the usher(for example,the link can be "www.ursite.com?act=active&userid=youdoit" -->this is the dangerous way,but u can do it better )
so when user click that link,change the activated to 1.
hope i did what u want
__________________
ParaSnake is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-26-2005   #5 (permalink)
Registered User
 
ViciOuS's Avatar
 
Join Date: Apr 2005
Posts: 82

Quote:
Originally Posted by Space Cowboy
I want to make
Code:
if($row['Activated'] > 0)
{
This work with the first php script in the above post! So that the account has to be activated before the login works!
Do what parasnake did, but instead of using an integer like that, you can use a string by enclosing something like act_false in single or double quotes.

example
Code:
$answer = 'act_false';
Then, encrypt the asnwer with md5. Now when you send this answer through HTTP, it will be encrypted. Although this way isn't the best, it is still effective and simple.
Also, make sure on the register page (or however your users register an account) that you pre-set the 'Activated' row to the encrypted answer that you want (i.e. - set the users 'Activated' row to $en_answer before you send the activation e-mail).

Code:
// define answer as the false answer
$answer = 'act_false';
$en_answer = md5($answer);

$link = '<a href="http://www.yoursite.com?answer=' . $en_answer . '&?user=' 
. $username.'" title="Activate your account!">Click this link to activate your account!</a>
You should be able to figure it out from there. Sometimes its best to try and figure things out for yourself so you can learn it better.
ViciOuS is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-26-2005   #6 (permalink)
Registered User
 
Space Cowboy's Avatar
 
Join Date: May 2005
Location: England (South)
Posts: 40

OK ill try and explain this a bit better. Im using http://biorust.com/index.php?page=tu...tail&tutid=115 that tutorial so the activation parts are all ready in place. I want to edit my own login page the top on i posted so that the part of the code in the tutorial noworks with my login php so that it only alows login if activated row =1! Thanks for the help here BTW!
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-30-2005   #7 (permalink)
code anyone?
 
scrowler's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Posts: 590

Send a message via MSN to scrowler Send a message via Skype™ to scrowler
i thought that my script did only allow you to login if the user is activated?

care to explain more?
__________________
BioRUST Tutorials - the birthplace
scrowler is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-30-2005   #8 (permalink)
Registered User
 
Space Cowboy's Avatar
 
Join Date: May 2005
Location: England (South)
Posts: 40

Yes scrowler youre code works completly and thank you for such an amazing tutorial. The problem im havving is that youre login script does not work with my flash project, I am unshure why this is. So i am trying to make the login script i am currently use work in the same way. MY login script currently connects to the database checks the username and password then tells flash either everthing is ok or login failed. Im trying to add the code from youre login script that checks if the user has activated there account or not.

This is the Login Script im currently using:
Code:
<? 

//Database Connectivity Variables//

$DBhost = "localhost";
$DBuser = "";
$DBpass = "";

$db_name = "";
$login_table = "Users"; 


//Create a connection to the MySQL Database//

$connection = @mysql_connect($DBhost,$DBuser,$DBpass) or die("Couldn't Connect.");
$db = @mysql_select_db($db_name, $connection) or die("Sorry, I could not select the requested Database ");
$password = md5($pass);
//SQL Querys
$sql = "SELECT * FROM $login_table WHERE username = \"$username\" and password = \"$password\"
";

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");



/*Now checking for the results of the query. if the results are not equal to zero then everything ok, anything else then the authentication failed*/

$num = mysql_num_rows($result); 

if ($num != 0) { 

   
// Send Results Back To Flash //  

echo "status=Authentication Accepted"; 
echo "&chklogin=good";
echo "username=$username";
exit;  
}
else {
// Send Errors Back To Flash //
 echo "chklogin=bad";
 echo "&status=Athentication Failed";  
}

?>
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-31-2005   #9 (permalink)
code anyone?
 
scrowler's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Posts: 590

Send a message via MSN to scrowler Send a message via Skype™ to scrowler
oh ok, make sure there is a column in your database called "Activated" (1 digit int: 0 for no or 1 for yes) then try this:

Code:
<? 

//Database Connectivity Variables//

$DBhost = "localhost";
$DBuser = "";
$DBpass = "";

$db_name = "";
$login_table = "Users"; 


//Create a connection to the MySQL Database//

$connection = @mysql_connect($DBhost,$DBuser,$DBpass) or die("Couldn't Connect.");
$db = @mysql_select_db($db_name, $connection) or die("Sorry, I could not select the requested Database ");
$password = md5($pass);
//SQL Querys
$sql = "SELECT * FROM $login_table WHERE username = '$username' AND password = '$password' AND Activated = 1 LIMIT 1";

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");

/*Now checking for the results of the query. if the results are not equal to zero then everything ok, anything else then the authentication failed*/

if (mysql_num_rows($result) > 0) { 

   
// Send Results Back To Flash //  

echo "status=Authentication Accepted"; 
echo "&chklogin=good";
echo "username=$username";
exit;  
}
else {
/* this will be triggered if the login is wrong, or if the user isn't activated */
// Send Errors Back To Flash //
 echo "chklogin=bad";
 echo "&status=Athentication Failed";  
}

?>
this will only return the user with correct username, password and activation status
__________________
BioRUST Tutorials - the birthplace
scrowler is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-31-2005   #10 (permalink)
Registered User
 
Space Cowboy's Avatar
 
Join Date: May 2005
Location: England (South)
Posts: 40

OMG thank you very much script works amazing thanks again scrowler!
Space Cowboy is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2005   #11 (permalink)
Invicible Snake
 
ParaSnake's Avatar
 
Join Date: Sep 2004
Location: Ho Chi Minh City,Vietnam
Posts: 668

Send a message via MSN to ParaSnake Send a message via Yahoo to ParaSnake
Remember to check everything before compile SCB
__________________
ParaSnake is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 06-01-2005   #12 (permalink)
code anyone?
 
scrowler's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Posts: 590

Send a message via MSN to scrowler Send a message via Skype™ to scrowler
glad to be of service
__________________
BioRUST Tutorials - the birthplace
scrowler is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Tutorial Trio! Man1c M0g Announcements 3 07-09-2005 09:15 AM
PHP template Help fewtion HTML / PHP / ASP / JS 8 02-26-2005 04:55 PM
PHP Tutorial Requests Man1c M0g BioRUST Specific Threads 6 06-17-2004 09:16 PM
PHP Help! Someone please help me LimitLess HTML / PHP / ASP / JS 12 05-16-2004 08:59 AM
Digital's Quick and Dirty Guide to PHP Order HTML / PHP / ASP / JS 15 05-05-2004 01:18 AM


All times are GMT +1. The time now is 05:36 PM.
Content Relevant URLs by vBSEO 3.2.0

Design & Content © BioRUST 2008 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT