Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

user authentication

Codes here !

Moderators: macek, egami, gesf

Postby wmdrumaizi » Sun Jul 06, 2003 7:16 pm

I think you should add one more fields in MySQL i.e password; I think e-mail is not quite confidential; but e-mail will also do. Here is my code; I use username (Login name) and pass (Password)

Code: Select all
<?php
//Start the output buffering
ob_start();
?>

<?php
start the session
session_start();
?>

<HTML>
<HEAD>
<title>My Website</title>
</head>
<body>

<?php
//declare the variables; host, username, password, dbase name
define ('HOST', 'localhost');
define ('USER', '');
define ('PASS', '');
define ('DB', 'researchers');

//If user hasn't logged in;
if (empty($HTTP_SESSION_VARS['username']))
{
  //connect to the database...
  mysql_connect(HOST, USER, PASS);
  mysql_select_db(DB);
  //...and get number of result matched
  $result = mysql_query("SELECT COUNT(*) AS numfound FROM maintable WHERE username='{$HTTP_POST_VARS['username']}' AND pass='{$HTTP_POST_VARS['pass']}'");
$result_ar = mysql_fetch_array($result);
  //If no result is matched
  if ($result_ar['numfound'] < 1)
       {
             //Redirect the user to error page to prompt him
    header('Location: erlogin.htm');
   exit;
       }
//If user logged in successfully; register his/her username
$username = $HTTP_POST_VARS['username'];
session_register('username');
   
//Say a few words to welcome them
echo "<Center><font face=\"verdana\" color=\"#000000\" size=\"4\">Welcome to My Website</font></Center>";
   
}
?>

</body>
</html>



To protect certain page, put this code above the <html> tag; i.e on top of your code
Code: Select all
<?php
ob_start();
session_start();
session_register('username');
?>

<?php
if (empty($HTTP_SESSION_VARS['username']))
 {
 header('Location: login.html');
 exit;
 }
?>



I hope this will help U, let me know if you're still having the problem.
This code is not secure enough to be used in online shopping..but it is enough for newbie...
User avatar
wmdrumaizi
New php-forum User
New php-forum User
 
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia

Return to mySQL & php coding

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.