Jump to content

Login system


freelancer

Recommended Posts

Well, I have been working out user management system which allows users to register on my clan website, also I'm using 1 php news script with what I add news to my main site and it has also commenting feature which doesn't need registration but I want that only people who has logged in can post comments. How should login script look like that only logged in people are allowed to see $comments area. News script is phpnews (http://newsphp.sourceforge.net) and its template based that where I type $comments it shows comments form. Maybe you guys can give some advices or any user management system which can be integrated with websites.

 

Hope you understood what I mean!

Link to comment
Share on other sites

i think it might be way easier for you to use some kind of CMS (Content Management System) there are a lot of free ones around: Joomla, Koobi, PHP-Nuke, e107, just to mention some of the most popular ones.

 

They have everything you need - user registrations, news posting, comments only for users etc and much much more.

Link to comment
Share on other sites

I generally use MYSQL and cookies -- You need a login form function - a database to store this info into and then the files which do all the work for you.

 

Normally i have a functions file which will contain the Login form and if a user isnt logged in you display the login form - if a user is logged in then you display the content a logged in member would see.  This can also be done with mutiple user levels see different content etc.

 

The files you would need (named whatever you like):

* Functions - contains a set of functions you can call like login form, forgotten password etc etc

* Login - the script which processes the login - user checking - form completeness etc

* loggedin - the login form is sent to here and sets cookies etc but only if the login form is valid

*usercheck - can be included anywhere you want a check to be made where logged in content should be displayed. - If the user is logged in it will display relevant content if they arent it will say "Need to be logged in"  in the area it is included.

 

you would also need to create a registration form, maybe a validation form, and maybe a lost password form.  It can become a bit of a headache. then you need if's buts and maybes :)

 

 

Link to comment
Share on other sites

Possibly not the best code in the world but here you go:

 

 

 

login.php

<?php

//Database/Mysql//
$dusername = "mysite_username";					//username to connect to database
$dpwd = "mysite_password";	 					//password to accecss mySQL
$dhost = "localhost";					//your db host usually localhost
$dbname = "mysite_database_1";					//db name to be accessed


$conn = mysql_connect("$dhost","$dusername","$dpwd") or die ("Unable to connect to database.");
$db=mysql_select_db($dbname,$conn) or die("Unable to connect to database!");

include("functions.php");
if($action==login)
{
if($_COOKIE[username]=="" AND $_COOKIE[password]=="")
{
$md5_password = md5($_POST[password]);
$sql = mysql_query("SELECT * FROM users WHERE username = '$_POST[username]' AND password = '$md5_password'") OR DIE("Sorry there is a mysql error.");
$row = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);
if($_POST[password]=="")
{
//include("$sitepath/html/header.php");
print("No password Entered.");
login_form();
print("<BR><BR> Lost Password?");
lostpass_form();
//include("$sitepath/html/footer.php");
exit;
}
elseif($_POST[username]=="")
{
//include("$sitepath/html/header.php");
print("No username Entered.");
login_form();
print("<BR><BR> Lost Password?");
lostpass_form();
//include("$sitepath/html/footer.php");
exit;
}




elseif($numrows == "0")
{
//include("$sitepath/html/header.php");
print("Username and Password doesnt match!");
login_form();
print("<BR><BR> Lost Password?");
lostpass_form();
//include("$sitepath/html/footer.php");
exit;
}

else
{
setcookie("username","$_POST[username]",time()+30000000);
setcookie("password","$md5_password",time()+30000000);
echo "<meta http-equiv=refresh content=\"2;url=$siteurl/loggedin.php\">";
//include("$sitepath/html/header.php");
print("your now being logged in!");
//include("$sitepath/html/footer.php");

}
}
else
{
//include("$sitepath/html/header.php");
print("Your are already loggedin!");
//include("$sitepath/html/footer.php");

}
}
if($action=="")
{
//include("$sitepath/html/header.php");
login_form();
print("<BR><BR> Lost Password?");
lostpass_form();
//include("$sitepath/html/footer.php");
}
?>

Link to comment
Share on other sites

functions.php

<?php
function lostpass_form()
{
global $siteurl,$sitetitle;
print("
<form name='form1' method='post' action='$siteurl/lostpw.php?action=lostpass'  name='lp_form' onSubmit=\"document.lp_form.lp_submit.disabled=true\">
  <table width='100%' border='0' cellspacing='0' cellpadding='4' align='center'>
    <tr> 
      <td>Email Address</td>
      <td><input name='email_address' type='text'></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name='recover' type='hidden' value='recover'>
        <input type='submit' name='lp_submit' value='Gain The Knowledge Of My Password!'></td>
    </tr>
  </table>
</form>
");

}


function login_form()
{
global $siteurl,$sitetitle,$username;
print("
<form action='$siteurl/login.php?action=login' method='post' name='l_form' onSubmit=\"document.l_form.l_submit.disabled=true\">
  <table width='50%' border='0' align='center' cellpadding='4' cellspacing='0'>
    <tr> 
      <td width='22%'>Username</td>
      <td width='78%'><input name='username' type='text'></td>
    </tr>
    <tr> 
      <td>Password</td>
      <td><input name='password' type='password'></td>
    </tr>
    <tr> 
      <td> </td>
      <td><input type='submit' name='l_submit' value='Login To Mysite!'></td>
    </tr>
  </table>
</form>
");
}
?>

Link to comment
Share on other sites

loggedin.php

 

<?php
include("config.php");

include("$sitepath/html/header.php");
include("user_check.php");

// I normally insert a refresf here if i dont wany to display anything
print("Your now logged in fully.<BR><BR>");

include("$sitepath/html/footer.php");
?>

Link to comment
Share on other sites

user_check.php

 

<?php

include_once("$sitepath/functions.php");
$sql = mysql_query("SELECT * FROM users WHERE username = '$_COOKIE[username]'") OR DIE("Sorry Theres a mysql error.");
$row = mysql_fetch_array($sql);
$num = mysql_num_rows($sql);
//check if username exsists
if($num == 0)
{
print("You must be logged in to veiw this page!<BR>");
login_form();
print("<BR>Lost password?");
lostpass_form();
include("$sitepath/html/footer.php");
exit;
}


//check cookies exsist
elseif($_COOKIE[username]=="" OR $_COOKIE[password]=="")
{
print("You must be logged in to veiw this page!<BR>");
login_form();
print("<BR>Lost password?");
lostpass_form();
include("$sitepath/html/footer.php");
exit;
}



//check if password is correct

elseif($_COOKIE[password] != $row[password])
{
print("You must be logged in to veiw this page!<BR>");
login_form();
print("<BR>Lost password?");
lostpass_form();
include("$sitepath/html/footer.php");
exit;
}
$username = $_COOKIE[username];
$date = date("U");
$updatelastlogin = mysql_query("UPDATE users SET last_login = '$date' WHERE username = '$_COOKIE[username]'");


$pm = mysql_query("SELECT * FROM im WHERE sentto = '$username' AND status = 'unread'");
$npms = mysql_num_rows($pm);
if($npms > 0)
{
print("<table class='all' width=500><tr><td><a href='$siteurl/pm/index.php'>You Have $npms New Message(s)</a></td></tr></table><BR>");
}
?>

 

Like i said - maybe not the best code but with a little playing around i am sure you can achieve what you want. Some of this stuff won't be relevant to your usage - you will just need to remove things that arent like included headers / footers / nav and any irrelevant database fields.

 

Cheers

Matt

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.