0

How do I create cookies which save the user agent and it's IP address at my login page. When I login, it will be save into my database row as IP and Agent. The login page match the current IP and Agent to with database saved data.

If data match login success else redirect to abcd.com.

Here I can make this code but I not under how to add cookies please help and try to make this.

My code is below:

<?php
$msg = "";

if (isset($_POST['submit'])) {
    $con = new mysqli('localhost', 'research_emailC', 'test123', 'research_phpEmailConfirmation');

    $email = $con->real_escape_string($_POST['email']);
    $password = $con->real_escape_string($_POST['password']);

    if ($email == "" || $password == "")
        $msg = "Please check your inputs!";
    else {
        $sql = $con->query("SELECT id, password, isEmailConfirmed FROM users WHERE email='$email'");
        if ($sql->num_rows > 0) {
            $data = $sql->fetch_array();
            if (password_verify($password, $data['password'])) {
                if ($data['isEmailConfirmed'] == 0)
                    $msg = "Please verify your email!";
                else {
                    $msg = "You have been logged in";
                }
            } else
                $msg = "Please check your inputs!";
        } else {
            $msg = "Please check your inputs!";
        }
    }
}
?>
  • Possible duplicate of [Assign null to a SqlParameter](https://stackoverflow.com/questions/4555935/assign-null-to-a-sqlparameter) – SpotDetector Aug 01 '18 at 15:36
  • http://php.net/manual/en/function.setcookie.php `setcookie()` to set a cookie, use `$_SERVER[]` for information such as user agent and potentially IP, but its not reliable. – Kisaragi Aug 01 '18 at 15:40
  • This will cause problems with validation btw - especially if you need to maintain that "session" across http and https versions of the domain. Load-balancers can cause the IP address to change and I found that, at some point in the past, AOL's own browser can actually change the UA string on the fly when moving between http and https (not sure if it's still the case) ... and yes, some few people do still use it. – CD001 Aug 01 '18 at 15:44
  • @DevBot How is this question a duplicate of that? – Bas van Dijk Aug 01 '18 at 15:46
  • @BasvanDijk [It's not](https://meta.stackexchange.com/questions/313474/unable-to-delete-comment-that-was-created-from-a-duplicate-flag) – Blue Aug 01 '18 at 15:46
  • You all wanna I not understand on which matter tou are talking together. What about mine. Please how I make .. please post your codes – Shubham Singhania Aug 01 '18 at 15:50
  • There is a manual for cookies http://php.net/manual/en/features.cookies.php - which is a good start and it contains examples in there for you to try out. – Funk Forty Niner Aug 01 '18 at 20:48
  • I read your manual it have basic but how I match it with my database . Why you not short code share. About how I match ing it, and how I add ip and agent info. – Shubham Singhania Aug 02 '18 at 00:47

0 Answers0