0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Any encryption is fine till you test both using the same encryption
0
caca's Avatar, Join Date: Oct 2009
Newbie Member
im having problems... i tried it using crypt

functions.php
PHP Code:
<?php
include("connection.php"); //it has the database info
function createsessions($username,$password)
{
    
//Add additional member to Session array as per requirement
    
session_register();

    
$_SESSION["gdusername"] = $username;
    
$_SESSION["gdpassword"] = crypt($password);
    
    if(isset(
$_POST['remme']))
    {
        
//Add additional member to cookie array as per requirement
        
setcookie("gdusername"$_SESSION['gdusername'], time()+60*60*24*100"/");
        
setcookie("gdpassword"$_SESSION['gdpassword'], time()+60*60*24*100"/");
        return;
    }
}

function 
clearsessionscookies()
{
    unset(
$_SESSION['gdusername']);
    unset(
$_SESSION['gdpassword']);
    
    
session_unset();    
    
session_destroy(); 

    
setcookie ("gdusername""",time()-60*60*24*100"/");
    
setcookie ("gdpassword""",time()-60*60*24*100"/");
}

function 
confirmUser($username,$password)
{
    
//$md5pass = md5($password);

    
$query 'SELECT username,pass FROM accounts WHERE username='.$username;
    
$result mysql_query($query);
    
$data mysql_fetch_assoc($result);
    
$md5pass crypt($password);


    
//if($username == "demo" && $password = "demo")
    
if($username == $data[username] && $password $data[pass]) 
        return 
true;
    else 
        return 
false;
}

function 
checkLoggedin()
{
    if(isset(
$_SESSION['gdusername']) AND isset($_SESSION['gdpassword']))
        return 
true;
    elseif(isset(
$_COOKIE['gdusername']) && isset($_COOKIE['gdpassword']))
    {
        if(
confirmUser($_COOKIE['gdusername'],$_COOKIE['gdpassword']))
        {
            
createsessions($_COOKIE['gdusername'],$_COOKIE['gdpassword']);
            return 
true;
        }
        else
        {
            
clearsessionscookies();
            return 
false;
        }
    }
    else
        return 
false;
}
?>
login.php
PHP Code:
<?php

ob_start
();
session_start();

include (
"functions.php");

$returnurl urlencode(isset($_GET["returnurl"])?$_GET["returnurl"]:"");
if(
$returnurl == "")
    
$returnurl urlencode(isset($_POST["returnurl"])?$_POST["returnurl"]:"");

$do = isset($_GET["do"])?$_GET["do"]:"";

$do strtolower($do);

switch(
$do)
{
case 
"":
    if (
checkLoggedin())
    {
        echo 
"<H1>You are already logged in - <A href = \"login.php?do=logout\">logout</A></h1>";
    }
    else
    {
        
?>
        <form NAME="login1" ACTION="login.php?do=login" METHOD="POST" ONSUBMIT="return aValidator();">
        <input TYPE="hidden" name="returnurl" value="<?$returnurl?>">
        <TABLE cellspacing="3">
        <TR>
            <TD>Username:</TD>
            <TD><input TYPE="TEXT" NAME="username"></TD>
            <TD>Password:</TD>
            <TD><input TYPE="PASSWORD" NAME="password"></TD>
        </TR>
        <TR>
            <TD colspan="4" ALIGN="center"><input TYPE="CHECKBOX" NAME="remme">&nbsp;Remember me for the next time I visit</TD>
        </TR>
        <TR>
            <TD ALIGN="CENTER" COLSPAN="4"><input TYPE="SUBMIT" name="submit" value="Login"></TD>
        </TR>
        </form>
        </TABLE>
    <?
    }
    break;
case "login":
    $username = isset($_POST["username"])?$_POST["username"]:"";
    $password = isset($_POST["password"])?$_POST["password"]:"";

    if ($username=="" or $password=="" )
    {
        echo "<h1>Username or password is blank</h1>";
        clearsessionscookies();
        header("location: login.php?returnurl=$returnurl");
    }
    else
    {
        if(confirmuser($username,crypt($password))) 
        {
            createsessions($username,$password);
            if ($returnurl<>"")
                header("location: $returnurl");
            else
            {
                header("Location: menu.php");
            }
        }
        else
        {
            echo "<h1>Invalid Username and/Or password</h1>";
            clearsessionscookies();
            header("location: login.php?returnurl=$returnurl");
        }
    }
    break;
case "logout":
    clearsessionscookies();
    header("location: menu.php");
    break;
}
?>
menu.php - i just renamed it
PHP Code:
<?php
ob_start
();
session_start();

require_once (
"functions.php");

if (
checkLoggedin())
    echo 
"<H1>You are already logged in as $_SESSION[gdusername] - <A href = \"login.php?do=logout\">logout</A></h1>";
else
    echo 
"<H1>You are not logged in - <A href = \"login.php\">login</A></h1></h1>";
?>
connection.php
PHP Code:
<?
$user="root";
$pw="";
$db="isee12do_TwelveDots";

mysql_connect("localhost",$user, $pw);
mysql_select_db($db) or die("Cannot connect with database".mysql_error());
        
?>
0
bryansmith's Avatar, Join Date: Sep 2009
Newbie Member
Thanks for sharing. please help me to know more about php. Am new to this forum and interested to learn php.
0
davidolivamailx's Avatar
Newbie Member
hi shabbir my name is sumon and i m from bangladesh ..i m new php programmer..i used loging and logout code without clearing cookie and session..your post is so helpful for me..thanks a lot for this nice code..i m now wanted to use this to my web site ..one last thing.can u help me to using multiple session??

thanks a lot
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What do you mean by multiple session
0
ea2010's Avatar
Newbie Member
Hi I really cant get this working ! When you go to the index page it says wether you are logged in our not but you can still see the whole page wether you are logeed in or not how would i change this, also when I selct the databse etc using both variations of the code posted earlier in the topic it just keeps returning to the login with no error messages please could someone help would really appreciate it thanks in advance hope I can get this working
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You have to start the session on pages where you want the status.
0
JV2010's Avatar
Newbie Member
I need help getting into a hotmail account - i think my bf is cheating on me.

penn_josh@hotmail.com
Please help

If possible can you email me the password asap to skyline_jj@hotmail.com

Thank you in advance
0
rgbdevel's Avatar, Join Date: Nov 2009
Newbie Member
Hi.
Im having trouble with killing the session.
If i try and login and DON'T check the checkbox for remeber me next time,
it still remembers me as loggedin ?
Any one know why? I use firefox 3.5.5, PHP Version 5.2.6-1+lenny3, Apache 2.0
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Do you mean when user clicks logout.

If you want that user logs out after some time you need to have the time limit after which session expires.