Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Login and Logout using Sessions and Cookies (http://www.go4expert.com/articles/login-logout-using-sessions-cookies-t361/)

ssk1712 26Jun2010 11:21

Re: Login and Logout using Sessions and Cookies
 
what exactly is the problem I cant make out see if you can help me.

shabbir 26Jun2010 11:31

Re: Login and Logout using Sessions and Cookies
 
Quote:

Originally Posted by ssk1712 (Post 69633)
yes I tried putting a bracket into the code but still the same thing.I dont think its going to work.

Looks like there is some other issue on your end because the same code works fine with me.

ssk1712 26Jun2010 11:45

Re: Login and Logout using Sessions and Cookies
 
Code:

<?php

function createsessions($username,$password)
{
    //Add additional member to Session array as per requirement
    session_register();

    $_SESSION["gdusername"] = $username;
    $_SESSION["gdpassword"] = md5($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); // Not needed any more as pointed by ted_chou12

    /* Validate from the database but as for now just demo username and password */
    if($username == "demo" && $password = "demo")
        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;
}
?>


index.php
PHP Code:
Code:

<?php
ob_start();
session_start();

require_once ("functions.php");

if (checkLoggedin())
    echo "<H1>You are already logged in - <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>";
?>


login.php
PHP Code:
Code:

<?php

ob_start();
session_start();

require_once ("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,md5($password))) // As pointed out by asgard2005
        {
            createsessions($username,$password);
            if ($returnurl<>"")
                header("location: $returnurl");
            else
            {
                header("Location: index.php");
            }
        }
        else
        {
            echo "<h1>Invalid Username and/Or password</h1>";
            clearsessionscookies();
            header("location: login.php?returnurl=$returnurl");
        }
    }
    break;
case "logout":
    clearsessionscookies();
    header("location: index.php");
    break;
}
?>


ssk1712 26Jun2010 11:46

Re: Login and Logout using Sessions and Cookies
 
this the code your about right?i checked it.

ssk1712 26Jun2010 11:49

Re: Login and Logout using Sessions and Cookies
 
this is the code your talking about right?i checked it.

ssk1712 26Jun2010 12:34

Re: Login and Logout using Sessions and Cookies
 
are you there?

ssk1712 26Jun2010 14:55

Re: Login and Logout using Sessions and Cookies
 
there is no error nor any output now.i modified the code bit.can you check and let me known.reply.

ssk1712 26Jun2010 16:06

Re: Login and Logout using Sessions and Cookies
 
Code:

<html x>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sessions Cookies</title>
</head>

<body>
<?php
//if (!isset($_GET['subpage'])) { 
  //if (isset($_GET['error']) && $_GET['error'] == '1') { 
  //?>
<!--<font color="#FF0000"><b>ERROR: </b>Invalid username and/or password. Please try again.</font> -->
 <? //} ?>
<form method="post" action="login.php" >
Username:<br />
<input type="text" name="username" maxlength="50">
<br /><br />
Password:<br />
<input type="password" name="password" maxlength="50">
<br /><br />
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
<?php
//}
 //else if (isset($_GET['subpage']) && $_GET['subpage'] == 'login')
 { 
  $user_name = "root";
  $password = ""; 
  $database = "members1"; 
  $server = "127.0.0.1"; 
 
 $db_handle = mysql_connect($server, $user_name, $password); 
  $db_found = mysql_select_db($database, $db_handle);
 
 
  $user = $_POST['username']; 
  $pass = $_POST['password']; 
  $db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "select * from users where user='$user' AND pass='$pass'";
$result = mysql_query($SQL);

 // $usercheck2 = mysql_query("select * from users where user='$user' AND pass='$pass'");
  //$usercheck = mysql_num_rows($SQL); 
  if ($usercheck > '0') {
    setcookie("user", $user, time()+60*60*24*30, "/", ".yoursite.com", 0); 
    setcookie("pass", $pass, time()+60*60*24*30, "/", ".yoursite.com", 0);
    header("Location: index.php"); 
  } else {
    //header("Location: login.php?error=1"); 
  }
 // mysql_close();
 }
}
?>


shabbir 26Jun2010 16:47

Re: Login and Logout using Sessions and Cookies
 
Quote:

Originally Posted by ssk1712 (Post 69639)
this is the code your talking about right?i checked it.

Yes

Quote:

Originally Posted by ssk1712 (Post 69647)
are you there?

This is not chat but a forum and that means you would not see me online but sooner or later I would reply and please maintain the decorum.

Quote:

Originally Posted by ssk1712 (Post 69649)
there is no error nor any output now.i modified the code bit.can you check and let me known.reply.

You have done lot of things like db connectivity and other things but let me know what you want to know.

ssk1712 26Jun2010 17:02

Re: Login and Logout using Sessions and Cookies
 
sorry for the mistake,no thanks I can handle it now.Thanks for help.


All times are GMT +5.5. The time now is 18:41.