Reputation: 102
<form action=userLogin.php method=post>
<div id="login">
<div id="submit" class="h">Log In</div>
Email
<br>
<input id="email" name="email" type="email" required>
<br>
Password
<br>
<input name="pass" type="password" required=required><br>
<a href="recover.php">Forgot your password</a><br>
<div id="submit">
<input type="checkbox"> Remember
<div id="h">
<input type="submit" value="Log in"></div>
</div>
</div>
<?php
if (empty($_POST['pass']) == false) {
$uname = $_POST['email'];
$pass = $_POST['pass'];
include("config.php");
$query = $mysqli->query("SELECT * FROM user WHERE email='$uname' AND password='$pass'");
$numrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query)) {
if ($numrows == 0) {
?>
<script>
$(document).ready(function () {
$("#alert").fadeIn();
});</script>
<?php
}
else {
$id = $row[id];
setcookie('userId', $id);
}
}
}
?>
This is my code which get user data and check whether the user exists or not and set cookie on the user's browser.
When i check that the cookie is set I could see only phpMyAdmin cookies not my userId cookie.You can see the screenshot and the browser got stucked and its automatically closed
But when i editted setcookie function with
setcookie('userId','$id');
the cookie was set but its value is %24id
[$id]. So how can i set the cookie in the value of the variable $row[id] is the error with my pc or server or with my code?
When i used chrome with $id without qoutes the error came This webpage is not available The webpage at localhost/mysite/mysite/userLogin.php might be temporarily down or it may have moved permanently to a new web address
Upvotes: 1
Views: 1691
Reputation: 1138
cookies have to be sent with headers.
"setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace."
http://php.net/manual/en/function.setcookie.php
Upvotes: 0