[PHP] Loginsystem.

Joined
Jul 16, 2008
Messages
861
Reaction score
25
Hi, i created my own Login system for my site:)
Here is script.
login.php
PHP:
<?
/**
 * @author hylperzone
 * @copyright 2009
 */
?>
<td rowspan="4" background="http://forum.ragezone.com/images/wut_12.png" width="446" height="354" id="nieuws"><center><form action="php/checklogin.php" method="post">
Gebruikersnaam:<input type="text" name="username" /><br />
Wachtwoord:<input type="password" name="password" /><br />
<input type="submit" name="login" value="Login" />
</form><div><a href="?page=Registreer">Nog geen Account klik hier om u te registreren!</a></div></center>
</td>

checklogin.php
PHP:
<?php

/**
 * @author hylperzone
 * @copyright 2009
 */
?>
<style type="text/css">
body{
    background-color: #211F1F;
    color:white;
}
#tablel{
    border:2px solid black;
    background-color: #400000;
}
A:link{
    color: silver;
}
A:hover{
    color:red;
}
A:Visited{
    color:silver;
}
A:active{
    color:red;
}
#ing{
    border:2px solid black;
    background-color: #400000;
}
</style>
<?
include('config.php');
$user = antisql($_POST['username']);
$pw = antisql($_POST['password']);
$query = mssql_query("SELECT * FROM login Where UserID='$user' AND password='$pw'");
$check = mssql_num_rows($query);
$ban = mssql_query("SELECT * FROM Account Where UserID='$user'");
$checkban = mssql_fetch_assoc($ban);
$grade = $checkban['UGradeID'];
if(!$check){
	fuckoff();
	}
elseif($grade == 253){
	ban();
}elseif($check == true){
?>
<table align="center" id="ing" width="800" border="1">
<tr><td>
Welkom <b><?=$user;?></b>
</td><td><?
if(!$_GET['page']){
 echo 'Home';
 }else{
 	echo $_GET['page'];
 }
?></td>
</tr>
<tr>
<td>
<a href="?page=Home">Home</a><br />
<a href="?page=changepw">Verander wachtwoord</a><br />
<a href="?page=deleteclan">Clan Verwijderen</a><br />
<a href="?page=maakclan">Clan Aanmaken</a>
</td><td><? include('cpanel\page.php'); ?></td>
</tr>
</table>
<?
}
?>
Page.php
PHP:
<?php

/**
 * @author hylperzone
 * @copyright 2009
 */

if($_GET['page']==''){
	include('cpanel/home.php');
}elseif($_GET['page']==Home){
	include('cpanel/home.php');
}elseif($_GET['page']==changepw){
	include('cpanel/changepw.php');
}elseif($_GET['page']==maakclan){
	include('cpanel/mc.php');
}elseif($_GET['page']==deleteclan){
	include('cpanel/dc.php');
}

?>
config.php
PHP:
<?php

/**
 * @author hylperzone
 * @copyright 2009
 */
 //Servers.
$myhost = '';
$odhost = '';
$mshost = '';
//Pw's.
$mypw = '';
$odpw = '';
$mspw = '';
//Inlogames.
$myname = '';
$odname = '';
$msname = '';
//ODBC database.
$oddb = 'GunzDB';
//Connection Strings.
$conms = mssql_connect($mshost,$msname,$mspw);
$conod = odbc_connect($oddb,$odname,$odpw);
$conmy = mysql_connect($myhost,$myname,$mypw);
//database connections.
$mydb = mysql_select_db('hylperzone',$conmy);

$msdb = mssql_select_db('GunzDB',$conms);

if(!$conms){
	echo 'Een fout in de verbinding(MSSQL).';
}
if(!$conmy){
	echo 'Een fout in de verbinding(MySQL).';
}
if(!$conod){
	echo 'Een fout in de verbinding(ODBC).';
}

function antisql($sql) {
$sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|--|\\\\)/"),"",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
$sql = addslashes($sql);
return $sql;
}
function gtfo($wut) {
echo "<div id='bg'><center><table width='' height='300'>
<tr>
<td align=center width='100%'><b><font color=\"#000000\">Bericht:</b></td>
</tr>
<tr>
<td width='100%'><center><font color=\"#000000\"><b>$wut</center></td>
</tr>
</table></div>";
die();
}


function checkemail($address) {
list($local, $host) = explode("@", $address);
$pattern_local = "^([0-9a-z]*([-|_]?[0-9a-z]+)*)(([-|_]?)\.([-|_]?)[0-9a-z]*([-|_]?[0-9a-z]+)+)*([-|_]?)$";
$pattern_host  = "^([0-9a-z]+([-]?[0-9a-z]+)*)(([-]?)\.([-]?)[0-9a-z]*([-]?[0-9a-z]+)+)*\.[a-z]{2,4}$";
$match_local = eregi($pattern_local, $local);
$match_host = eregi($pattern_host, $host);
if($match_local && $match_host) {
return 1;
}
else {
return 0;
}
}


function num_rows(&$rid) {
$num= odbc_num_rows($rid);
if ($num >= 0) {
return $num;
}

if (!odbc_fetch_row($rid, 1)) {
odbc_fetch_row($rid, 0);
return 0;
}

if (!odbc_fetch_row($rid, 2)) {
odbc_fetch_row($rid, 0);
return 1;
}

$lo= 2;
$hi= 8192000;

while ($lo < ($hi - 1)) {
$mid= (int)(($hi + $lo) / 2);
if (odbc_fetch_row($rid, $mid)) {
$lo= $mid;
} else {
$hi= $mid;
}
}
$num= $lo;
odbc_fetch_row($rid, 0);
return $num;
}
function fuckoff(){
echo "<center><table id='tablel'><tr><td><p align='center'>Login:</p</td></tr<tr><td><font color='red'>Verkeerde Gebruikersnaam / Wachtwoord!</font></td></tr><tr><td><p align='center>'><a href='../index.php?page=Login'>Klik hier om opnieuw in te loggen.</a></p></td></tr></table></center>";
}
function ban(){
echo "<center><table id='tablel'><tr><td><p align='center'>Login:</p</td></tr<tr><td><font color='red'>U bent gebanned op NLGunZ u bent hier niet meer welkom!!</font></td></tr><tr><td><p align='center>'><a href='../index.php?page=Login'>Klik hier om opnieuw in te loggen.</a></p></td></tr></table></center>";
}
?>
site:
http://nlgunz.sytes.net/nlgunz/wut/index.php?page=Login
You can Login with
Username = ragezone
Password = ragezone1

The problem, if i click on a <a href=""></a> in the menu. It gonna automatic back to the error Message.
And it won't include the things i asked for -,- i dunno how to make my login now fully work -,-
Any more information ask.
 
Well ofcourse it doesn't work, you never save the information that the current user is logged in!
You should use sessions.

Im trying but im still learning sessions, and if i want a Cookie that saves the Login
How i use that in my script -,-


uhh in the checklogin in

elseif($check == true){
$time = 60 * 60 * 24 * 7 + time();
setcookie($user, date("G:i - m/d/y"), $time);
setcookie($pw, date("G:i - m/d/y"), $time);

Should that work?
 
Sessions use cookies btw.

You can edit cookies with Tamper Data Tought so,
i will use Sessions:)

PHP:
<?php
session_start(); // start up your PHP session! 
/**
 * @author hylperzone
 * @copyright 2009
 */
include('config.php');
$user = antisql($_POST['username']);
$pw = antisql($_POST['password']);
session_register($user);
session_register($pw);
?>
Thats the beginnning now of checklogin.php
 
checklogin.php
PHP:
<?php
session_start(); // start up your PHP session! 
/**
 * @author hylperzone
 * @copyright 2009
 */
include('config.php');
$user = antisql($_POST['username']);
$pw = antisql($_POST['password']);
$_SESSION['user'] = $user;
$_SESSION['pw'] = $pw;
$users = $_SESSION['user'] = $user;
$pws = $_SESSION['pw'] = $pw;
?>
<style type="text/css">
body{
    background-color: #211F1F;
    color:white;
}
#tablel{
    border:2px solid black;
    background-color: #400000;
}
A:link{
    color: silver;
}
A:hover{
    color:red;
}
A:Visited{
    color:silver;
}
A:active{
    color:red;
}
#ing{
    border:2px solid black;
    background-color: #400000;
}
</style>
<?
$query = mssql_query("SELECT * FROM login Where UserID='$users' AND password='$pws'");
$check = mssql_num_rows($query);
$ban = mssql_query("SELECT * FROM Account Where UserID='$users'");
$checkban = mssql_fetch_assoc($ban);
$grade = $checkban['UGradeID'];
if(!$check){
	fuckoff();
	}
elseif($grade == 253){
	ban();
}elseif($check == true){
?>
<table align="center" id="ing" width="800" border="1">
<tr><td>
Welkom <b><?=$user;?></b>
</td><td><?
if(!$_GET['page']){
 echo 'Home';
 }else{
 	echo $_GET['page'];
 }
?></td>
</tr>
<tr>
<td>
<a href="?page=Home">Home</a><br />
<a href="?page=changepw">Verander wachtwoord</a><br />
<a href="?page=deleteclan">Clan Verwijderen</a><br />
<a href="?page=maakclan">Clan Aanmaken</a><br />
<a href="loguit.php">Log uit</a>
</td><td><? include('cpanel\page.php'); ?></td>
</tr>
</table>
<?
}
?>
Same problem dunno how to make it save the session:p or load it just 1 time -,-
i also created a Logout.php:
PHP:
<?php

/**
 * @author hylperzone
 * @copyright 2009
 */

session_start();
session_destroy();
header("LOCATION: ../index.php");

?>
 
Sessions are as safe as cookies, they ain't safe at all. That's why you should use an anti-sql-injection function over EVERY but not limited to $_GET, $_POST, $_SERVER, $_COOKIE, $_SERVER.

Anyway. Don't use that much variables. Slows everything down. You can implent things like cookies in queries without storing them in variables first... why not do it?
 
Sessions are as safe as cookies, they ain't safe at all. That's why you should use an anti-sql-injection function over EVERY but not limited to $_GET, $_POST, $_SERVER, $_COOKIE, $_SERVER.

Anyway. Don't use that much variables. Slows everything down. You can implent things like cookies in queries without storing them in variables first... why not do it?

Umm you don't use variables.. because their slow..?
:closedeyes:

I understand that using redundant variables is slower.. and more work.. eele, dude you should reduce the amount of variables in your code. There's no reason to use $user or $pass, just bypass it completely because that's what a hacker would do anyway..
PHP:
$user = antisql($_POST['username']); 
$pw = antisql($_POST['password']); 
$_SESSION['user'] = $user; 
$_SESSION['pw'] = $pw; 
$users = $_SESSION['user'] = $user; 
$pws = $_SESSION['pw'] = $pw;
to this:
PHP:
$_SESSION['user'] = antisql($_POST['username']); 
$_SESSION['pw'] = antisql($_POST['password']);

In fact, go through all of your pages and do a find & replace for $user/$pw and replace them with $_SESSION['user']/$_SESSION['pw']..

There's no shortcut to cleaning up old redundant code, take it from me. Attempting to not use variables at all is retarded.. Everything is powered by variables (please challenge me on this.. Chemistry, biology, and of course, computers), and $_SESSION['user'] is a superglobal variable.. Functions are ways to manipulate variables.. Arrays are lists full of variables.. MySQL, XML, CSV, etc are all databases of variables.. There would be no programming without variables, so you must use them. The key is learning how to use as little code as possible for all of your projects. No redundant variables like the ones above. No long single-purpose functions. More short, useful, get-to-the-point functions to replace long essays of repeating code. If your PHP looks like a paper with 100 lines of 'I will not throw sand at my teacher', you have a problem.

Glad to see progress though :thumbup:
 
Try to Use this index.php.
Code:
<table width="300" align="center" cellpadding="0" cellspacing="1" bgcolor="gray" border="0">
<tr><th><a href="http://forum.ragezone.com"><img src="http://forum.ragezone.com/images/totallypro/misc/header.gif" border="0"></a></th></tr>
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div align="center">Made and Designed by : Leon</div>

and this checklogin.php
PHP:
<?php
include("config.php");


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// 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);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {

        echo '
        Wrong username or password!<br /><a href="index.php">Go Back</a>
        
        ';

    }
?>

and some of this config.php

PHP:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="attendance"; // Database name
$tbl_name="members"; // Table name
?>
 
Sessions are as safe as cookies, they ain't safe at all.
Session data is stored server side, cookie data is stored client side. Cookies can be manipulated, sessions cannot. I see a major difference here.
 
Thnx for all your answers :D

@ LeonStrife
The login is for mssql ;) If i want some **** like that there enough sites on google, but ty for trying to help me.

@s-p-n

Im gonna try to slow down the Variables and trying to get it work with sessions :)
 
Back