Need some help

PHP coding talk.

Moderator: Moderators

Need some help

Postby Mr-Slowly on Sat Mar 17, 2007 8:02 pm

Greetings

i'm looking for a login script that uses sessions (id's)
and cookies

where can i get a good member system

or a good tutorial

tnx in advance :wink:
God Bless You
Mr-Slowly
Registered User
Registered User
 
Posts: 29
Joined: Wed Mar 07, 2007 12:15 pm

Postby GraphicMD on Sun Mar 18, 2007 4:39 am

This is for PHP4. I am not sure if it will work in PHP5 or not.

login.php
Code: Select all
<?php
session_start();

include 'lib/user.php';

$user = new user();

if (isset($_GET['logout']))
   $user->logout();

if (isset($_POST['login']))
   $user->login();

include 'style/login.html';
?>


lib/user.php
Code: Select all
<?php

class user
{
   var $error = '';

   function user()
   {
      if (!isset($_SESSION['loggen_in']))
         $_SESSION['loggen_in'] = false;
   }

   function login()
   {
      if ( $_POST['username'] == 'admin' && $_POST['password'] == 'admin' )
      {
         $_SESSION['username'] = $_POST['username'];
         $_SESSION['loggen_in'] = true;
      }
      else
         $this->error = 'Incorrect username and/or password.';
   }

   function logout()
   {
      $_SESSION['loggen_in'] = false;
      $_SESSION['username'] = "";
   }
}
?>



style/login.html
Code: Select all
<html>
<head>
   <title>Login Script</title>
</head>
<body>
   <!-- IF LOGGED IN <?php if ($_SESSION['loggen_in']) { ?> -->

   <div>Thank you <b><?= $_SESSION['username'] ?></b> for loggin in.</div>
   <div><a href='?logout'>logout</a></div>

   <!-- ELSE LOGGED OUT <?php } else { ?> -->

   <div><?= $user->error ?></div>
   <form method="post" action="">
   <label for="username">Username</label>: <input type="text" name="username" id="username" />
   <label for="password">Password</label>: <input type="password" name="password" id="password" />
   <div><input type="submit" value="login" name="login" /></div>
   </form>

   <!-- END IF <?php } ?> -->
</body>
</html>
User avatar
GraphicMD
Designer
Designer
 
Posts: 254
Joined: Sat Jun 26, 2004 3:30 pm

Postby Mr-Slowly on Sun Mar 18, 2007 7:10 pm

EDIT:


Tnx m8 it works fine for me to :wink:
Last edited by Mr-Slowly on Mon Mar 19, 2007 9:08 am, edited 1 time in total.
God Bless You
Mr-Slowly
Registered User
Registered User
 
Posts: 29
Joined: Wed Mar 07, 2007 12:15 pm

Postby GraphicMD on Mon Mar 19, 2007 2:25 am

I just tested it in PHP5 and it works fine.
User avatar
GraphicMD
Designer
Designer
 
Posts: 254
Joined: Sat Jun 26, 2004 3:30 pm

Postby Mr-Slowly on Mon Mar 19, 2007 5:55 pm

somehow it doest work like this
login.php
Code: Select all
<?php
//***********************/
//    Filename: login.php
//    Version:    0.1
//    Author:    Rucell
//***********************/
session_start();

define('IN_R', true);
$r_root_path = './';
include($r_root_path . 'common.php');
include($r_root_path . 'includes/page_header.php');

$template->set_filenames(array(
      'body' => 'login_body.tpl'
));

 
if (isset($_GET['logout']))
   {
      $user->logout();
   }

elseif (isset($_POST['login']))
   {
      $user->login();
   }
else
   {
   $template->assign_block_vars("login", array( 
           'L_LOGIN' => $lang['Login'],
         'LOGIN_ACTION' => 'login.php' 
       ));
   }
$template->pparse('body');

include($r_root_path . 'includes/page_tail.php');

?>

login_body
Code: Select all
<!-- BEGIN login -->
<form action="{login.LOGIN_ACTION}" method="post">

<table width="40%" cellspacing="0" class="forumline">
<tr>
   <td class="row-header"><span>{login.L_LOGIN}</span></td>
</tr>
<tr>
   <td class="row1g" style="padding: 20px; padding-top: 30px;" align="center"><div style="margin: auto; width: 250px"><table cellpadding="3" cellspacing="1">
   <tr>
      <td align="right" nowrap="nowrap"><span class="gen">Username:</span></td>
      <td><input type="text" name="username" class="post" size="25" maxlength="40" /></td>
   </tr>
   <tr>
      <td align="right" nowrap="nowrap"><span class="gen">Password:</span></td>
      <td><input type="password" name="password" class="post" size="25" maxlength="32" /></td>
   </tr>
   <tr align="center">
      <td colspan="2">{HIDDEN_FIELDS}<input type="submit" name="login" class="mainoption" value="Login" /></td>
   </tr>
   <tr align="center" nowrap="nowrap">
      <td colspan="2"><span class="gensmall"><a href="{SEND_PASSWORD}" class="gensmall">Lost pass ?</a> | <a href="{U_REGISTER}" class="gensmall">Register</a></span></td>
   </tr>
   </table></div></td>
</tr>
</table>

</form>
<!-- END login -->

user_class
Code: Select all
<?php
//***********************/
//    Filename: user_clas.php
//    Version:    0.1
//    Author:    Rucell
//***********************/

class user 

   var $error = ''; 

   function user()
   { 
       if (!isset($_SESSION['loggen_in']))
           $_SESSION['loggen_in'] = false;
   }

   function login()
   {
      $username = $_POST['username'];
      $password = md5($_POST['password']);
      
      $sql = mysql_query('SELECT * FROM users WHERE "username" = "$username"');
      $row = mysql_fetch_array($sql);

      if($password == $row['password'] && $username == $row['username'])
      {
         $_SESSION['username'] = $username;
         $_SESSION['loggen_in'] = true;
           setcookie (COOKIE_NAME, $_POST['username'],time()+3600*1);
           echo 'U bent ingelogt.';
           echo '<META HTTP-EQUIV=\'REFRESH\' CONTENT=\'1; URL=\'index.php\'>';
      }            
      else
         $this->error = 'Incorrect username and/or password.';
   }

   function logout()
   {
      $_SESSION['loggen_in'] = false;
      $_SESSION['username'] = "";
   }
}

?>
God Bless You
Mr-Slowly
Registered User
Registered User
 
Posts: 29
Joined: Wed Mar 07, 2007 12:15 pm

Postby DooBDee on Mon Mar 19, 2007 6:04 pm

Silly question, but you are including that class definition in your login.php file?

Also add mysql_real_escape_string() around $username.
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby Mr-Slowly on Mon Mar 19, 2007 6:13 pm

you mean like this ?

Code: Select all
  function login() 
  { 
       $username = mysql_real_escape_string($_POST['username']);


still not working :(
God Bless You
Mr-Slowly
Registered User
Registered User
 
Posts: 29
Joined: Wed Mar 07, 2007 12:15 pm

Postby UseLess on Mon Mar 19, 2007 6:34 pm

Greetings,

Try this;
[code start="18"]function login()
{
$username = isset($_POST['username']) ? htmlspecialchars(stripslashes($_POST['username'])) : '';
$password = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';

if( !empty($username) && !empty($password) )
{
$sql = mysql_query("SELECT * FROM users WHERE username = '$username'");
$row = mysql_fetch_array($sql);

if($password == $row['password'] && $username == $row['username'])
{
$_SESSION['username'] = $username;
$_SESSION['loggen_in'] = true;
setcookie (COOKIE_NAME, $username, time()+3600*1);

echo 'U bent ingelogt.';
echo '<meta http-equiv="refresh" content="1; url=index.php">';
}
else
{
$this->error = 'Incorrect username and/or password.';
}
}
else
{
$this->error = 'username and password are empty';
}
}[/code]
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby Mr-Slowly on Mon Mar 19, 2007 6:48 pm

wow it works nice

thanx m8

and thanx for the quick reply :wink:
God Bless You
Mr-Slowly
Registered User
Registered User
 
Posts: 29
Joined: Wed Mar 07, 2007 12:15 pm


Return to PHP Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron