954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Php Help: Login, Logout Script

hello everyone i am currently working on my website. I wanted to make a php script for registering users. So that they may log in, log out, register, and access member only functions.

I found a good tutorial online but since im fairly new to php and have never worked with SQL i dont have a good idea of what i am doing.

In the website below the user says to make a SQL table so we can keep track of all the members. I have gone into my webhosting control panel and have made a new SQL table but i have no way of modfying it with what he says to put in it.

http://www.trap17.com/forums/Php-Simple-Login-Tutorial-t7887.html

Name the table "dbUsers." It will need 4 fields:

<em>Name             Type                 Addition</em>
id                          int(10)              Primary Key, AUTO_INCREMENT
username            varchar(16)        Unique
password            char(16)          
email                   varchar(25)


Can anyone help me out with this? Im really confused :(

jhonnyboy
Junior Poster in Training
97 posts since Jul 2008
Reputation Points: 11
Solved Threads: 0
 

You must be able to edit the fields in the table you've created.
What are you using to create your databases phpmyadmin ?

nunomartinsinet
Newbie Poster
6 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

you have created your table correctly.now you want to input records into that table.first ill tell you the concept of login,logout and register processes.
when a user tries to login to your site you must check that there is a match with the database data and the user input data.if the password and username matches you allow him to login.
there for the first step is you must allow users to register.assume there is a simple html page with a form for that.
(create separate files for code which separated from ####### marks,save files with the given extention)

########form.html########

<html>
<body>
<form action="register.php" method="post">
<input type="text" name="username" id="username">
<input type="password" name="pw" id="pw">
<input type="text" name="fname" id="fname">
<input type="text" name="email" id="email">
<input type="submit">
</form>
</body>
</html>


#######register.php######

<?php
mysql_connect("your_host_name","your_username","your_password");
mysql_select_db("your_database_name");

mysql_query("INSERT INTO dbusers(name,username,password,email) VALUES('$_POST[fname]','$_POST[username]','$_POST[pw]','$_POST[email]')") or die("cannot execute the query");

echo "user registered";

?>


registration process is finished.now you should create a login form.

######loginform.html######

<html>
<body>
<form action="login.php" method="post">
<input type="text" name="username" id="username">
<input type="password" name="pw" id="pw">
<input type="submit">
</form>
</body>
</html>


when you click on the submit button following login script will run

######login.php#######

<?php
mysql_connect("your_host_name","your_username","your_password");
mysql_select_db("your_database_name");

$myusername=$_POST['username'];
$mypassword=$_POST['pw'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$mysql = "SELECT * FROM dbusers WHERE username='$myusername' and password='$mypassword' ";

$result = mysql_query($mysql) or die("cannot execute query");

$count = mysql_num_rows($result);

if($count==1)
{
  session_register('username');
  header("location:home.php"); // put your home page neme here
}

else
echo "login fail";

?>


]

#####logout.php#####

<?php
session_start();
session_destroy();
echo "successfuly logout";
?>


use this code at the top of your home page to obtain session variable

<?php
session_start();
$username = $_SESSION['username'];
?>


now you can use "$username" variable to display username of the logged in user.
ex.

<?php
      echo "you logged in as ".$username;
      ?>


thats all

anuspan
Newbie Poster
5 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

how can i link to logout from logout script

ambujh
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

i want logout script in php

ambujh
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

If you're using hyperlink to logout, then direct your link to logout.php

forumforme123
Newbie Poster
6 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
logout:

echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";

this will destroy the session and return to your index.php file.



Logout.php
<?php
session_start();
session_unset();

session_destroy();
// Logged out, return home.
Header("Location: index.php");
?>
phpbeginners
Posting Whiz in Training
226 posts since Jul 2009
Reputation Points: 12
Solved Threads: 32
 

can somebody help me i need a compiling site for documents its like a registration site but your filling up documents for a business. that every page has a login/logout. so that if she/he didn't finish the registration he can logout to save and login to continue thank you

ampam
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

please do email thx

ampam
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

how we have to link the logout script with logout link and where it will redirect

dev.vini85@gmai
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
can somebody help me i need a compiling site for documents its like a registration site but your filling up documents for a business. that every page has a login/logout. so that if she/he didn't finish the registration he can logout to save and login to continue thank you

And how much are you going to pay for that? :D
And thank you for hijacking the thread.

Now seriously: Start your thread and post what you have and your programming ability level ;)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
logout:

echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";

this will destroy the session and return to your index.php file.



Logout.php
<?php
session_start();
session_unset();

session_destroy();
// Logged out, return home.
Header("Location: index.php");
?>


session_unset(); is not necessary as session_destroy(); does it all ;)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

i have an error in checklogin.php in $count it says Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given. how can i fix this?

nico24
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

i am geting following error Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at

aruna_juice
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You