0

I have a code like this

ajax.js

var xmlHttp = buatObjekXmlHttp();

function buatObjekXmlHttp()
{
var obj = null;
if(window.ActiveXObject)
    obj = new ActiveXObject("Microsoft.XMLHTTP");
else
    if(window.XMLHttpRequest)
        obj = new XMLHttpRequest();
if(obj == null)
    document.write("Browser tidak mendukung XMLHttpRequest");
return obj;
}

function ambilData(sumber_data, id_elemen)
{
if(xmlHttp != null)
{
    var obj = document.getElementById(id_elemen);
    xmlHttp.open("GET", sumber_data);

    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
            obj.innerHTML = xmlHttp.responseText;
    }
}
xmlHttp.send(null);
}

cekpass.php

<!DOCTYPE HTML>
<html>
<head>
    <title>
        Memeriksa Password
    </title>
    <script src="ajax.js"></script>
    <script>
        function prosesData(sumber_data, id_elemen)
        {
            if(xmlHttp != null)
            {
                var elemen_div = document.getElementById(id_elemen);
                var elemen_user_id = document.getElementById("user_id");
                var elemen_password = document.getElementById("password");
                var url = sumber_data + "?user_id = " + elemen_user_id.value + "&password = " + elemen_password.value;
                xmlHttp.open("GET", url);
                xmlHttp.onreadystatechange = 
                function()
                {
                    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
                    {
                        if(xmlHttp.responseText == "OK")
                        {
                            elemen_div.innerHTML = "OK";
                            location.href="infoweb.php";
                        }
                        else
                            elemen_div.innerHTML = "User_id atau/dan password salah";
                    }
                }
                xmlHttp.send(null);
            }               
        }
    </script>
</head>
<body>
    <form>
        User ID : <input type="text" name="textuser_id" id="user_id">
        <br>
        Password : <input type="password" name="textpassword" id="password">
        <br>
        <input value="Login" type="button" onclick="prosesData('password.php', 'id_info');">
    </form>
    <div id="id_info"></div>
    <p id="demo"></p>
</body>
</html>

And this

password.php

<?php
header("Cache-Control : no-cache, must-revalidates");
header("Expires : Mon, 26 Jul 1997 00:00:00 GMT");

//Data user-id dan password
$user[] = "anton"; $pass[] = "biola";
$user[] = "ahmad"; $pass[] = "gitar";
$user[] = "dewi"; $pass[] = "piano";
$user[] = "santi"; $pass[] = "keyboard";
$user[] = "salman"; $pass[] = "perkusi";

//Peroleh variable URL
$user_id = $_GET["user_id"];
$password = $_GET["password"];

$ok = FALSE;
for($i=0; $i < count($user); $i++)
{
    if(($user_id == $user[$i]) && ($password == $pass[$i]))
    {
        $ok = TRUE;
        break;
    }
}

session_start(); //Buat sesi
$_SESSION["ok"] = $ok;

if($ok)
{
    $_SESSION["user_id"] = $user_id;
    $_SESSION["password"] = $password;
    print("OK");
}
else
    print("NOT OK");
?>

I want to make simple login using ajax with javascript, not with database but with array, if it's correct, it will link to another page, but the problem is, when i put correct answer it always said i'm wrong :( can someone tell me what went wrong with my code :( I'm still new :(

2 Answers 2

1

Your url is malformed. Remove unnecessary spaces by changing

var url = sumber_data + "?user_id = " + elemen_user_id.value + "&password = " + elemen_password.value;

to

var url = sumber_data + "?user_id=" + elemen_user_id.value + "&password=" + elemen_password.value;

Sign up to request clarification or add additional context in comments.

1 Comment

Ma god, so it just because of space O.O thnx you very much for the help :)
0

Rather than go into possible solutions to yur matching array index problem, I'm just going to show you a solution that wil work.

<?php
header("Cache-Control : no-cache, must-revalidates");
header("Expires : Mon, 26 Jul 1997 00:00:00 GMT");

//Data user-id dan password
$user["anton"] = "biola";
$user["ahmad"] = "gitar";
$user["dewi"] = "piano";
$user["santi"] = "keyboard";
$user["salman"] = "perkusi";

//Peroleh variable URL
$user_id = $_GET["user_id"];
$password = $_GET["password"];

$ok = FALSE;
if($user[$user_id] === $password){
    $ok = TRUE;
}

session_start(); //Buat sesi
$_SESSION["ok"] = $ok;

if($ok)
{
    $_SESSION["user_id"] = $user_id;
    $_SESSION["password"] = $password;
    print("OK");
}
else
    print("NOT OK");
?>

This is no more insecure than your existing method and it works.

1 Comment

It's working too, and more short, thnx a lot :) but i can only thumb up, can't checklist more than 1 answerr, sorry :( but thnx very much for the help and insight to the new method, it's great :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.