0

I'm trying to code a page that checks a database if a users ip address exists, if not it offers them to signup in a form, if the user does exist then it offers a "play" button.

For some odd reason it's not working, I have an account in the DB but it still won't offer the "play" button. I've also tried signing up with the form I've made, but the page doesn't do anything when I click submit.

<?php
    $con = mysqli_connect("localhost","root","","********");
    $ip = $_SERVER['REMOTE_ADDR'];

    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //Checking DB Connection//
    $result = mysqli_query($con,"SELECT * FROM users WHERE ip='$ip'");

    while($row = mysqli_fetch_array($result)) {
        $rsname = $row['rsname'];
        $tokens = $row['tokens'];
    }

    if (mysqli_num_rows($result) > 0) {
        if ($tokens >= 1) {
            echo '<h2>Hello '.$rsname.'! Click The Button Below To Play!';
            echo '<a href="slots.php"><button>Play!</button></a>';
        }
        else {
            echo '<h2>Your Account "'.$rsname.'" Already Exists, But You Do Not Have Any Tokens.</h2><br />';
            echo '<h3>You Can Purchase More Token From "Got Your IP" In Game For 20k/Token.</h3>';
        }
    }
    else if (isset($_POST['rsname'])) {
        $sql="INSERT INTO users (rsname, ip)
        VALUES
        ('$_POST[rsname]','$_SERVER[REMOTE_ADDR]')";

        if (!mysqli_query($con,$sql)) {
            die('Error: ' . mysqli_error($con));
        }

        echo '<h2>Your account has been setup! Click the button below to play! </h2>';
        echo '<a href="slots.php"><button>Play!</button></a>';
    }
    else {
        echo '<h2>You Need To Register To Play RSLOTTO.</h2>';
        echo '<form id="form1" name="form1" method="post" action="play.php">';
        echo 'RuneScape Username:';
        echo '<input name="rsname" type="text" id="rsname" size="32" maxlength="40" />';
        echo '<input name="submit" type="button" value="submit" /></form>';
        echo 'We only ask for your username and not a password, because we tie your username to     your IP address so you don\'t need to enter a password. Safe as can be! :D';
    }

    mysqli_close($con);
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    Ok, so...where's the error? – David Morabito Sep 09 '13 at 01:49
  • 4
    Identifying a client using their IP address does not work. IP addresses change every so often. Some clients may also have dynamic IPs, which change all the time, or they may even be using a proxy. – Sverri M. Olsen Sep 09 '13 at 01:51
  • @SverriM.Olsen is absolutely right there. An IP address is never a reliable value for visitor identification. There are to many cases where an IP is not unique to a specific visitor, or even unique to a *single* visitor. – Atli Sep 09 '13 at 02:08
  • im using ip address to get the script started from there ill add more of what i need. But for now does anyone know why it's not identifying that i have an account in the db? – Matthew Myers Sep 09 '13 at 02:59
  • Are you per chance behind a caching proxy with your ISP? (if yes, `$_SERVER[REMOTE_ADDR]` might not be what you think it is) – Tim Post Sep 09 '13 at 04:51

1 Answers1

0

Checking a client using IP address is not a good scenario. Most users will be having IP address that changes dynamically.

Here a good solution would be to set a cookie in user browser that will not expire. So when the user visits your page again, you can check for the cookies which are set. If the cookie exist, then show the play button or else the registration page. This solution has also many downfalls. But we need to go with that. If the user deletes the browser cookie or so, then we have no other choice. This is not a best solution, but you can try this.

Please look at this thread for how you can identify unique visitors : Detecting a "unique" anonymous user

In your case the form is not submitting, because you are using input type="button".

<input type="button" /> buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application.

<input type="submit"> buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.

In your case,

<input name="submit" type="button" value="submit" />

should be

<input name="submit" type="submit" value="submit" />

So your form should be :

echo '<form id="form1" name="form1" method="post" action="play.php">';
echo 'RuneScape Username:';
echo '<input name="rsname" type="text" id="rsname" size="32" maxlength="40" />';
echo '<input name="submit" type="submit" value="submit" /></form>';

Hope this helps you :)

Community
  • 1
  • 1
Sabari
  • 6,205
  • 1
  • 27
  • 36