Results 1 to 5 of 5
  1. #1
    Junior Member Array
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    username and password verification

    hi all,
    my database name is "test".in the "test" database
    i have created a table name called "log" which has two fields
    namely username and password.
    i have given username as "admin" and password as "admin123".
    now i have written one code using php so that when i click the submit button both the username as admin and password as admin123 gets matched then it should direct to the next page...
    tell me how to check whether username and passwords are matching and if it matches it must point to the action part what we give in <form method="POST" action="www.php">
    kindly tell me what i must add to the below program.....
    below is the code in php......
    Code:
    <?php
    $host="localhost";  
    $username="root";  
    $password="";  
    $db_name="test"; 
    $tbl_name="log"; 
    
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
     
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword'];
    
    
    $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";
    }
    ?>

  2. #2
    WTF Master Array
    Join Date
    Nov 2009
    Posts
    1,903
    Thanks
    105
    Thanked 52 Times in 39 Posts


    You already have it set to check if it matching or not:

    Code:
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql);
    I'm confused what you need.

  3. #3
    Junior Member Array
    Join Date
    Dec 2010
    Location
    Poland
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Login to the site.


    firstly you need 2 files
    index.php and login.php


    index.php (start page, if not loged redirect to login.php If loged index.php show start page)
    --------------------------------------


    <?php
    mysql_query("SET NAMES 'utf8'");
    mysql_select_db($database_Logowanie, $Logowanie);
    if (!isset($_SESSION)) {
    session_start();
    }
    $MM_authorizedUsers = "";
    $MM_donotCheckaccess = "true";


    // *** Restrict Access To Page: Grant or deny access to this page
    function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
    // For security, start by assuming the visitor is NOT authorized.
    $isValid = False;


    // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
    // Therefore, we know that a user is NOT logged in if that Session variable is blank.
    if (!empty($UserName)) {
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
    // Parse the strings into arrays.
    $arrUsers = Explode(",", $strUsers);
    $arrGroups = Explode(",", $strGroups);
    if (in_array($UserName, $arrUsers)) {
    $isValid = true;
    }
    // Or, you may restrict access to only certain users based on their username.
    if (in_array($UserGroup, $arrGroups)) {
    $isValid = true;
    }
    if (($strUsers == "") && true) {
    $isValid = true;
    }
    }
    return $isValid;
    }


    $MM_restrictGoTo = "login.php";


    if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
    $MM_qsChar = "?";
    $MM_referrer = $_SERVER['PHP_SELF'];
    if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
    if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
    $MM_referrer .= "?" . $QUERY_STRING;
    $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
    header("Location: ". $MM_restrictGoTo);
    exit;
    }


    $colname_Recordset_logindata = "-1";
    if (isset($_SESSION['MM_Username'])) {
    $colname_Recordset_logindata = $_SESSION['MM_Username'];
    }
    $query_Recordset_logindata = sprintf("SELECT * FROM users WHERE login = %s", GetSQLValueString($colname_Recordset_logindata, "text"));
    $Recordset_logindata = mysql_query($query_Recordset_logindata, $Logowanie) or die(mysql_error());
    $row_Recordset_logindata = mysql_fetch_assoc($Recordset_logindata);




    $MM_qsChar = "?";
    $MM_referrer = $_SERVER['PHP_SELF'];
    if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
    if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
    $MM_referrer .= "?" . $QUERY_STRING;
    $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
    header("Location: ". $MM_restrictGoTo);


    //initialize the session
    if (!isset($_SESSION)) {
    session_start();
    }


    // ** Logout the current user. **
    $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
    if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
    $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
    }


    if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
    //to fully log out a visitor we need to clear the session varialbles
    $_SESSION['MM_Username'] = NULL;
    $_SESSION['MM_UserGroup'] = NULL;
    $_SESSION['PrevUrl'] = NULL;
    unset($_SESSION['MM_Username']);
    unset($_SESSION['MM_UserGroup']);
    unset($_SESSION['PrevUrl']);

    $logoutGoTo = "login.php";
    if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
    }
    }
    ?>

    // login.php

    <?php
    require_once('Connections/Logowanie_DB.php');


    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }


    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);


    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }


    // *** Validate request to login to this site.
    if (!isset($_SESSION)) {
    session_start();
    }


    $loginFormAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
    $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    }


    if (isset($_POST['login'])) {
    $loginUsername=$_POST['login'];
    $password=md5($_POST['passwd']);
    $MM_fldUserAuthorization = "";
    $MM_redirectLoginSuccess = "index.php";
    $MM_redirectLoginFailed = "login.php";
    $MM_redirecttoReferrer = false;
    mysql_select_db($database_Logowanie, $Logowanie);

    $LoginRS__query=sprintf("SELECT login, passwd FROM users WHERE login=%s AND passwd=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

    $LoginRS = mysql_query($LoginRS__query, $Logowanie) or die(mysql_error());
    $loginFoundUser = mysql_num_rows($LoginRS);
    if ($loginFoundUser) {
    $loginStrGroup = "";

    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;


    if (isset($_SESSION['PrevUrl']) && false) {
    $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
    }
    header("Location: " . $MM_redirectLoginSuccess );
    }
    else {
    header("Location: ". $MM_redirectLoginFailed );
    }
    }
    session_start();
    ?>
    <form ACTION="<?php echo $loginFormAction; ?>" METHOD="POST" class="clean" id="logowanie">
    <ol>
    <li style="">
    <fieldset>
    <legend>Logowanie</legend>
    <ol>
    <li>
    <label for="login">Login</label>
    <input type="text" id="login" name="login" value="" />
    </li>
    <li>
    <label for="passwd">Hasło</label>
    <input type="password" id="passwd" name="passwd" value="" />
    </li>
    </ol>
    </fieldset>
    </li>
    </ol>
    <?php
    mysql_free_result($Recordset_data);
    ?>

    You remember password in database (md5)


    Ask me help you

  4. #4
    WTF Lurker Array
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Your each and every code is correct but what you are to exact ally asking.

  5. #5
    WTF Groupie Array
    Join Date
    Apr 2013
    Location
    Sydney
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts


    Thanks to all of your for sharing this useful script, its really so useful for me.


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •