简体   繁体   中英

Unable to SET Cookie in PHP using $id

Unable to set $id as cookie value

$id is echoing properly. when I put a string inside cookie value it is working perfectly. Also I tested by placing cookie value with $cname its works perfect. But only when I try to place $id in place of cookie value, the cookie is not setting up.

if(isset($_POST['login'])){

$id = $_GET['id'];
$cname = "aff_id";
setcookie($cname, $id, time()+(60*60));

$email = $_POST['email'];
$pass = $_POST['pass'];
$query = mysqli_query($dbc, 'SELECT * FROM users WHERE email ="'. $email.'" and password ="'.$pass.'"');
if(mysqli_num_rows($query) == 1) {

    $result = mysqli_fetch_assoc($query);
    $_SESSION['uid'] = $result['id'];
    header('Location: admin.php');

} else {echo 'failed to login';}
}

Debug this sort of issue from the client side, because, "that's where it actually happens."

A cookie is only actually "set" by a browser when it receives a Set-Cookie: header from the host. It will then include that cookie in subsequent HTTP requests that it sends to the host. In other words, "there must be at least one round-trip."

Therefore, turn on the debugging facilities of your browser, then walk through the login sequence (after "purging the browser cache"), and see if in fact such an HTTP round-trip actually occurs. I'm sure that you'll discover that it did not, and/or you'll stumble into JavaScript error messages (that you didn't yet know about ...) along the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM