Solution: Using Cookies

Here are the solutions to the previous tutorial, ‘Briefing: Using Cookies’. If you missed this tutorial, you can find it here.

scookies-1

(note that there is more than one way to complete a task – I’m just giving you an idea of how to do it)

Task One

The first task asked you to create a single input that set a single cookie.

form.php

<html>
	<body>
		<form method="post" action="submit.php">
			Email: <input type="text" name="email" <?php if( isset( $_COOKIE['email'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['email'] ) . '"'; ?> /><br />
			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

submit.php

<?php

	if( $_POST['email'] ) setcookie( 'email', $_POST['email'], time()+86400 );
	echo "<a href=\"form.php\">form.php</a>";

?>

You can view this solution in action here.

Task Two

The second task was pretty much the same as the first, just with multiple inputs.

form.php

<html>
	<body>
		<form method="post" action="submit.php">
			Username: <input type="text" name="username" <?php if( isset( $_COOKIE['username'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['username'] ) . '"'; ?> /><br />
			Password: <input type="text" name="password" <?php if( isset( $_COOKIE['password'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['password'] ) . '"'; ?> /><br />
			Email: <input type="text" name="email" <?php if( isset( $_COOKIE['email'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['email'] ) . '"'; ?> /><br />
			Boy or Girl: <input type="text" name="gender" <?php if( isset( $_COOKIE['gender'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['gender'] ) . '"'; ?> /><br />
			Interests: <input type="text" name="interests" <?php if( isset( $_COOKIE['interests'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['interests'] ) . '"'; ?> /><br />
			Favorite Music: <input type="text" name="music" <?php if( isset( $_COOKIE['music'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['music'] ) . '"'; ?> /><br />
			Favorite Movies: <input type="text" name="movies" <?php if( isset( $_COOKIE['movies'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['movies'] ) . '"'; ?> /><br />
			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

submit.php

<?php

	$username = setcookie( 'username', $_POST['username'] );
	$password = setcookie( 'password', $_POST['password'] );
	$email = setcookie( 'email', $_POST['email'] );
	$gender = setcookie( 'gender', $_POST['gender'] );
	$interests = setcookie( 'interests', $_POST['interests'] );
	$music = setcookie( 'music', $_POST['music'] );
	$movies = setcookie( 'movies', $_POST['movies'] );

	echo "<a href=\"form.php\">form.php</a>";

?>

You can view this solution in action here.

Task Three

The final task was a modification of the second. This isn’t the cleanest way to do it, but it gets the job done.

form.php

<html>
	<body>
		<form method="post" action="submit.php">
			Username: <input type="text" name="username" <?php if( isset( $_COOKIE['username'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['username'] ) . '"'; ?> />
				 <?php if( $_COOKIE['username'] == 'Austin' ) echo 'Hey, you stole my name'; ?><br />

			Password: <input type="text" name="password" <?php if( isset( $_COOKIE['password'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['password'] ) . '"'; ?> />
				 <?php if( $_COOKIE['password'] == 'hunter2' ) echo 'you can go hunter2 my hunter2-ing hunter2'; ?><br />

			Email: <input type="text" name="email" <?php if( isset( $_COOKIE['email'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['email'] ) . '"'; ?> />
				 <?php if( $_COOKIE['email'] == 'ennadoha_694@yopmail.com' ) echo 'Send me an email!'; ?><br />

			Boy or Girl: <input type="text" name="gender" <?php if( isset( $_COOKIE['gender'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['gender'] ) . '"'; ?> />
				<?php if( $_COOKIE['gender'] == 'Girl' ) echo 'AY girl, when you gonna let me tap that'; ?><br />

			Interests: <input type="text" name="interests" <?php if( isset( $_COOKIE['interests'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['interests'] ) . '"'; ?> />
				 <?php if( $_COOKIE['interests'] == 'Guitar' ) echo '*shreds*'; ?><br />

			Favorite Music: <input type="text" name="music" <?php if( isset( $_COOKIE['music'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['music'] ) . '"'; ?> />
				 <?php if( $_COOKIE['music'] == 'Radiohead' ) echo 'I wish I was special'; ?><br />

			Favorite Movies: <input type="text" name="movies" <?php if( isset( $_COOKIE['movies'] ) ) echo 'value="' . htmlspecialchars( $_COOKIE['movies'] ) . '"'; ?> />
				 <?php if( $_COOKIE['movies'] == 'Fight Club' ) echo 'The first rule of Fight Club is...'; ?><br />

			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

The second file, submit.php, is the same as in task two.

You can view this solution in action here.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>