A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a php program to store the username in cookie and check whether the user have successfully login or not
Q:

Write a php program to store the username in cookie and check whether the user have successfully login or not

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 -

<?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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now