Set a cookie to store login detail
Write a php program to store the username in cookie and check whether the user have successfully login or not.
Cookies are pieces of content that are sent to a user's web browser. To set a cookie, we must call the setcookie() function before sending any other content to the browser, because a cookie is actually part of the header information.
Syntax of setcookie()
setcookie(name, value, expire, path, domain, secure, httponly);
The following example demonstrates how to store the username in a cookie and check whether that user successfully login or not -
<?php $submitted = isset($_POST['username']) && isset($_POST['password']); if($submitted) { setcookie('username', $_POST['username']); } ?> <!Doctype> <html> <head> <title>User Authentication</title> </head> <body> <?php if ($submitted): ?> <p>Hello <b><?php echo $_POST['username']; ?></b></p> <?php else: ?> <p>Login First</p> <?php endif; ?> </body> </html>
In the above code, we have set a variable to know if we submitted a login or not, and sets the username in cookie.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Solution
Cookies are pieces of content that are sent to a user's web browser. To set a cookie, we must call the setcookie() function before sending any other content to the browser, because a cookie is actually part of the header information.
Syntax of setcookie()
setcookie(name, value, expire, path, domain, secure, httponly);
The following example demonstrates how to store the username in a cookie and check whether that user successfully login or not -
In the above code, we have set a variable to know if we submitted a login or not, and sets the username in cookie.
need an explanation for this answer? contact us directly to get an explanation for this answer