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

Help with login system

Codes here !

Moderators: macek, egami, gesf

Help with login system

Postby KevinHoughton » Thu May 31, 2012 9:06 am

Alright, hi there.

I have to be honest, I'm not a proffesional PHP & MySQL scripter so that's why i am asking help here.
I am trying to create a login system.

Code: Select all
<?php

define('INCLUDE_CHECK',true);

require 'connect.php';

if($_POST['submit']=='Login')
{
   // Checking whether the Login form has been submitted
   
   $err = array();
   // Will hold our errors
   
   
   if(!$_POST['username'] || !$_POST['password'])
      $err[] = 'All the fields must be filled in!';
   
   if(!count($err))
   {
      $_POST['username'] = mysql_real_escape_string($_POST['username']);
      $_POST['password'] = mysql_real_escape_string($_POST['password']);
      $_POST['rememberMe'] = (int)$_POST['rememberMe'];
      
      // Escaping all input data
      
      
      $row = mysql_fetch_assoc(mysql_query("SELECT * FROM PlayerData WHERE Name='{$_POST['username']}' AND Password='{$_POST['password']}'"));

      if($row['Name'])
      {
         // If everything is OK login
         
         $_SESSION['Name']=$row['Name'];
         $_SESSION['rememberMe'] = $_POST['rememberMe'];
         
         // Store some data in the session
         
         setcookie('tzRemember',$_POST['rememberMe']);
      }
      else $err[]='Wrong username and/or password!';
   }
   
   if($err)
   $_SESSION['msg']['login-err'] = implode('<br />',$err);
   // Save the error messages in the session

   header("Location: index.php");
   exit;
}

session_name('tzLogin');
// Starting the session

session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks

session_start();

if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])
{
   // If you are logged in, but you don't have the tzRemember cookie (browser restart)
   // and you have not checked the rememberMe checkbox:

   $_SESSION = array();
   session_destroy();
   
   // Destroy the session
}


if(isset($_GET['logoff']))
{
   $_SESSION = array();
   session_destroy();
   
   header("Location: index.php");
   exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transporter Roleplay</title>
<style type="text/css">
a:link {
   text-decoration: none;
}
a:visited {
   text-decoration: none;
}
a:hover {
   text-decoration: none;
}
a:active {
   text-decoration: none;
}
</style>
</head>
<style type="text/css">
* {
   margin: 0;
   padding: 0;   
}

body {
   background-image: url(images/background.jpg);
   background-attachment:fixed;
}

#right {
   float: right;   
}
#center {
   border-radius: 0px 0px 10px 10px;
   width: 1200px;
   border-left-width: 3px;
   border-left-color: #666;
   border-left-style: solid;
   border-right-width: 3px;
   border-right-color: #666;
   border-right-style: solid;
   border-bottom-style: solid;
   border-bottom-color: #666;
   border-bottom-width: 3px;
   min-height: 900px;
   margin: auto;
   background-color: white;
   margin-bottom: 50px;
}

#header {
   width: 1200px;
   height: 150px;
   background-image:url(images/header.jpg);
}

.navbar {
   height: 40px;
   background-image:url(images/navbar.png);
}

.navbar ul {
   min-width: 100px;
   color: white;
   font-weight: bold;
   font-family: verdana;
   padding-top: 10px;
   height: 30px;   
   float: left;
   text-align: center;
   margin-left: 10px;
   padding-left: 10px;
   padding-right: 10px;
}

.navbar ul:hover {
   background-image: url(images/navbar_hover.jpg);   
}

footer {
   width: 100%;
   height: 30px;
   bottom: 0;
   padding-top: 10px;
   background-image:url(images/footer.jpg);
   position: fixed;
   text-align: center;
   color: black;
   font-weight: bold;
   font-family:Verdana, Geneva, sans-serif;
   margin-top: 50px;
}
</style>
<body>
<div id="center">
  <div id="header"></div>
  <div class="navbar">
     <a href="http://transrp.tk/index.php"><ul>Home</ul></a>
    <a href="http://transrp.tk/forums/"><ul>Forums</ul></a>
    <a href="http://transrp.tk/staff.php"><ul>Server team</ul></a>
    <a href="#"><ul>Community</ul></a>
    <a href="#"><ul>Patrocinations</ul></a>
    <a href="http://transrp.tk/donate.php"><ul>Donations</ul></a>
    <a href="#"><ul>About us</ul></a>
    <a href="samp://5.39.11.59:7777"><ul id="right">Click here to play now!</ul></a>
  </div>


<center><form class="clearfix" action="" method="post">
               <h1>Member Login</h1>
                   
                    <?php
                  
                  if($_SESSION['msg']['login-err'])
                  {
                     echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>';
                     unset($_SESSION['msg']['login-err']);
                  }
               ?>
               
               <label class="grey" for="username">Username:</label>
               <input class="field" type="text" name="username" id="username" value="" size="23" />
               <label class="grey" for="password">Password:</label>
               <input class="field" type="password" name="password" id="password" size="23" />
                  <label><input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" value="1" /> &nbsp;Remember me</label>
                 <div class="clear"></div>
               <input type="submit" name="submit" value="Login" class="bt_login" />
            </form>
            
             <li>Hello <?php echo $_SESSION['Name'] ? $_SESSION['Name'] : 'Guest';?>!</li>
            </center>
</div>
<footer>Transporter Roleplay &copy; 2012, All rights reserved.</footer>
</body>
</html>

You can check the log-in @ transrp.tk with demo-account: "demo" and password: "demo1234"

It will not work because I actually don't know, that's why I posted my script here.

I hope someone can teach me how to fix this, or just help me.

Many thanks, Kevin

PS. I am new on this forum so I don't know if this is the correct section to post it.
KevinHoughton
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu May 31, 2012 9:01 am

Re: Help with login system

Postby Nullsig » Thu May 31, 2012 10:54 am

This line: "session_start();" Needs to be the first line of the PHP script.
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI


Return to mySQL & php coding

Who is online

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

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