-1

I'm little bit confused how to log-in based on country or territory, what I'm talking about is like this:

The User table is:

id | username | password | level | id_country

The Country table is:

id | country_name

There is a drop-down/combo-box in a form, that filled with user countries, like this:

Country: [____] --> This part is automatically filled/disabled field if a user log-in with their username based on level and countries.

Province: [____] --> This is a chained combo-box from countries

City: [____] --> Also this chained to Province

What I mean is, when user's log-in with their country id, so the "Country Combo-Box" will be automatically filled and disabled. So no other user can't choose another country, only their country based on username and territories.

Thank you for all your help.

Best regards,

Kris

I have this scripts:

login_form.php

<div><center>
<form name="logForm" method="post" action="login_validation.php">
  <table class="table-list" width="500" border="0" cellpadding="2" cellspacing="1" bgcolor="#999999">
    <tr>
      <td width="106" rowspan="5" align="center" bgcolor="#CCCCCC"><img src="images/padlock.png" width="116" height="75" /></td>
      <th colspan="2" bgcolor="#CCCCCC"><b>LOGIN FORM </b></td>      
    </tr>
    <tr>
      <td width="117" bgcolor="#FFFFFF"><b>Username</b></td>
      <td width="263" bgcolor="#FFFFFF"><b>: 
        <input name="txtUser" type="text" size="30" maxlength="20" />
      </b></td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"><b>Password</b></td>
      <td bgcolor="#FFFFFF"><b>: 
        <input name="txtPassword" type="password" size="30" maxlength="20" />
      </b></td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"><b>Access Level</b></td>
      <td bgcolor="#FFFFFF"><b>:
        <select name="comboLevel">
        <option value="BLANK">- Choose -</option>
        <?php
        $level = array("operator", "admin");
        foreach ($level as $p) {
            if ($_POST['comboLevel']==$p) {
                $check="selected";
            } else { $check = ""; }
            echo "<option value='$p' $check>$p</option>";
        }
        ?>
        </select>
      </b></td>
      </tr>
    <tr>
      <td bgcolor="#FFFFFF">&nbsp;</td>
      <td bgcolor="#FFFFFF"><input type="submit" name="btnLogin" value=" Login " /></td>
    </tr>
  </table>
</form>
</center></div>

,and this script for validation:

login_validation.php

<?php 
if(isset($_POST['btnLogin'])){
    $msgError = array();
    if ( trim($_POST['txtUser'])=="") {
        $pesanError[] = "Username </b> cannot empty !";     
    }
    if (trim($_POST['txtPassword'])=="") {
        $msgError[] = "Password </b> cannot empty !";       
    }
    if (trim($_POST['comboLevel'])=="BLANK") {
        $msgError[] = "Level</b> not picked !";     
    }

    $txtUser    = $_POST['txtUser'];
    $txtUser    = str_replace("'","&acute;",$txtUser);

    $txtPassword=$_POST['txtPassword'];
    $txtPassword= str_replace("'","&acute;",$txtPassword);

    $comboLevel =$_POST['comboLevel'];

    if (count($msgError)>=1 ){
        echo "<div class='mssgBox'>";
        echo "<img src='images/exclamation.png'> <br><hr>";
            $noMsg=0;
            foreach ($msgError as $index=>$show_msg) { 
            $noMsg++;
                echo "&nbsp;&nbsp; $noMsg. $show_msg<br>";  
            } 
        echo "</div> <br>"; 



        include "login.php";
    }
    else {

        $loginSql = "SELECT * FROM user WHERE username='".$txtUser."' 
                    AND password='".md5($txtPassword)."' AND level='$comboLevel'";
        $loginQry = mysql_query($loginSql, $conndb)  
                    or die ("Query Error : ".mysql_error());


        if (mysql_num_rows($loginQry) >=1) {
            $loginData = mysql_fetch_array($loginQry);
            $_SESSION['SES_LOGIN']  = $loginData['id_user']; 
            $_SESSION['SES_USER']   = $loginData['username']; 


            if($comboLevel=="admin") {
                $_SESSION['SES_ADMIN'] = "admin";
            }


            if($comboLevel=="operator") {
                $_SESSION['SES_OPERATOR'] = "operator";
            }

            // Refresh
            echo "<meta http-equiv='refresh' content='0; url=?page=Main-Page'>";
        }
        else {
             echo "You Not Login As ".$_POST['comboLevel'];
        }
    }
} 
?>
Kris
  • 109
  • 1
  • 11
  • 1
    I'm a bit confused to determine what's your asking? Can you rephrase it? – jean Feb 13 '15 at 16:53
  • @jean Hi, thanks, actually it's like this: For example user A lived in Nigeria, so when user A log-in, there's a combo-box will automatically filled with user A country and it's disabled, so user A will not be able to choose the other countries. And there's user B and lived in India, so when user B log-in, the combo-box in the entry data form will atomatically choosed India and also disabled. – Kris Feb 13 '15 at 16:58
  • 2
    Ok, I'm pretty sure everyone got that. The problem is: what's your problem? Where are you stuck? Was you tasked to build it and don't know where to start? – jean Feb 13 '15 at 17:11
  • @jean I just updated my thread/question. I just don't know how to add country_id into my validation script and when user's log-in the combo-box in the data entry form will follow/automatically filled as the users come from/lived. So they will not necessary to choose their country from the combo-box. – Kris Feb 13 '15 at 17:48
  • So you're asking how to geolocate a user automatically? – deceze Feb 14 '15 at 03:01

1 Answers1

0

I am not very clear exactly what you wish to do. I am however going to offer two solutions just in case any of them helps.

First of all, if you want to geolocate a user using the IP address, you can refer to the solution given from this link. Get Country of IP Address with PHP

Secondly, if you capture the user's location during registration, I believe you have that data sitting in your database. You might want to modify your query as follows:

$loginSql = "SELECT user.id, country.country_name as country_name FROM user, country WHERE username='".$txtUser."' 
                AND password='".md5($txtPassword)."' AND level='$comboLevel' AND user.id_country = country.id";
// I am only fetching user.id and country.country_name assuming that's your focus for now. 
// You can add all other columns as you want.

While populating the data on your user's home page, you first make the combo-box <select disabled>. Then you loop through the available countries and mark the option value that matches the fetched (country_name) as selected.

I hope either one helps.

Community
  • 1
  • 1
Michael Woyo
  • 138
  • 2
  • 15
  • Thank you, first of all I have a form to entry for users, like this: Year: [ ComboBox ] Country: [ ComboBox ] Province: [ ComboBox ] City: [ ComboBox ] Now for example, I was registered as a user from Nigeria. Then when I filled username and password like usual login form, then go to the data entry form page like above, and will look like this after I succeed login : Year: [ ComboBox ] Country: [ Nigeria ] --> disabled ComboBox that automatically filled. Province: [ ComboBox ] City: [ ComboBox ] – Kris Feb 14 '15 at 06:51
  • Little bit confused, because I have this chainned combo-box and I have to connect the user session and the combo-box, as you can see in this link: http://i59.tinypic.com/dbsetd.jpg this my combo-box script, and I got javascript to run it (the chainned method), – Kris Feb 14 '15 at 11:46
  • As you can see in this link: http://i59.tinypic.com/jb7jt4.jpg also. (Just click the image to enlarge) – Kris Feb 14 '15 at 12:04