Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Login Script

Status
Not open for further replies.

chmanio

Programmer
Dec 16, 2002
20
0
0
PH
Hi!
its my first time to use PHP, can somebody please teach me to create a simple LOGIN Script? i have mySQL as my back-end.. i have a table named users (uid, password, fullname). Pls help..

Thanks in ADvance

Chris
 
It depend of what you want to do, do you want a secure logging method or just a method to log yourself?

I hope you allready know all about sessions if you want to create a log stuff.
 
just a simple login script. check username from the table and validate if the password is correct..
 
try something similar to this
use a login page which passes the variables $password and $fullname from a form, send it to loginfuction.php

Code:
<form action="[URL unfurl="true"]http://www.yourdomain.com/functions/loginfunction.php"[/URL] method="POST" name="LogIn">
        <label>Fullname:&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="fullname" type="text" id="fullname" tabindex="1" size="10"></label><br>

<label>Password:
<input name="password" type="password" id="password" tabindex="2" size="10"></label>
      <br>
	  <input name="Submit" value="Submit">
</form>

this is loginfuction.php, put it in your functions folder

Code:
<?php 
//select database and connect - you will need your own conn //script here, or put in the values in like("databasename"," ")
mysql_select_db($database_name, $connection);
//check fullname and password against the db 
//if yes return true
//else return false

//check if the fullname exists
$result = mysql_query("select uid, password from users where fullname='$fullname' ");


//IF THERE IS RECORDS RETURNED THEN
if ($row = mysql_fetch_array($result)) {
// IF THERE PASSWORD MATCHES THE DB PASSWORD THEN
  if (trim($row["password"]) == $password) {
  //START A SESSION
  session_start();
$HTTP_SESSION_VARS['userid'] = $row["id"];
   //TAKE THEM TO THE LOGGED IN PAGE
    header("Location: [URL unfurl="true"]http://www.yourdomain.com/members/index.php");[/URL]
}
//if the password is wrong take them back to the index page
else {
header("Location: [URL unfurl="true"]http://www.yourdomain.com/index.php?errormessage=2");[/URL]
}
}
//if the fullname is wrong or the password is not filled in take them back to index page 
else {
header("Location: [URL unfurl="true"]http://www.yourdomain.com/index.php?errormessage=1");[/URL]
}
?>

on every memberspage add this code

Code:
<?php
include('protector.php');
?>

create protector.php and place it in the members directory

Code:
<?php
session_start();
if($_SESSION['userid'] == "") { 
  header("Location: [URL unfurl="true"]http://www.icesplice.co.nz/test-webzings/index.php?errormessage=3");[/URL]
} 
?>



----------------------------------------
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
opps, the last bit of code header location- I didn't edit, you need to put your domain name etc instead of my testing one.
also the error messages.

in the very top of your index.php (where you are sending people if they fail to log in properly) before you even start the <html> tag put

Code:
<?php $errortext = "";

if ($errormessage == "1") {
  $errortext = "Invalid Fullname or Not Registered";
}

if ($errormessage == "2") {
  $errortext = "Invalid Password";
}

if ($errormessage == "3") {
  $errortext = "You are not Logged in";
}
?>

then put the echo of the error text wherever you want it to appear within the body of your page

Code:
<strong><font color="#FF0000"><?php echo $errortext ?></font></strong>

----------------------------------------
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top