Skip to main content

How to store user details in cookie and retrieve it during signup

Today we will practice how to store user cookie in our signup application and retrieve then when the form was correctly filled. This will assure visitors that their details was successfully received and that they can login at that moment. For testing purpose, copy the code below or write it manually, save it as cookietest.php and run it.

<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> <fieldset style="border-radius:5px;"> <legend>Register your profile today</legend> <label>Select profile picture</label><br> <input type="file" name="file" id="image" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Full name</label><br> <input type="text" name="fullname" id="fullname" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Full address</label><br> <textarea name="fulladdress" id="fulladdress" rows="5" cols="5" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value="">Your community, local government and state</textarea><br> <label>Email address</label><br> <input type="email" name="emailaddress" id="emailaddress" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Phone number</label><br> <input type="number" name="phonenumber" id="phonenumber" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Username</label><br> <input type="text" name="username" id="username" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Password</label><br> <input type="password" name="password" id="password" style="width:100%; padding:15px; border:none; display:block; font-size:18px; border-radius:5px;" value=""><br> <label>Select project type</label> <select name="project" style="width:100%; display:block; border:none; border-radius:5px; font-size:18px; margin-bottom:15px; padding:15px;"> <option>Select project type</option> <option value="Web technology developer">Web technology developer</option> <option value="Server-side technology developer">Server-side technology developer</option> <option value="Database management developer">Database management developer</option> <option value="Local and public network developer">Local and public network developer</option> </select> <input type="hidden" name="subscription" id="subscription" value="0.00"> <label>Gender</label> <input type="radio" name="radio" id="radiobuttonone" style="width:; padding:0; border:none; display:inline-block; font-size:18px; border-radius:0;" value="Male">Male <input type="radio" name="radio" id="radiobuttontwo" style="width:; padding:0; border:none; display:inline-block; font-size:18px; border-radius:0;" value="Female">Female</br> <p><input type="hidden" name="success" value="success"></p> <input type="submit" name="submitData" value="Register" style="margin-top:15px; padding:15px; width:45%; float:left; cursor:pointer; color:white; display:block; background:#04AA6D; border:none; font-size:18px; border-radius:5px;"> <input type="reset" onclick="window.location='index.php'" value="Cancel" style="margin-top:15px; padding:15px; width:45%; float:right; cursor:pointer; color:white; display:block; background:#04AA6D; border:none; font-size:18px; border-radius:5px;"> </fieldset> </form>

Our php script to submit users details and store copy in cookie container

Again, copy the below php script and paste it on top of your html file combine it with html above and save it as cookietest.php and run the code.

<!--php if(!empty($_POST["success"])) { setcookie ("image", $_POST["fullname"], time()+ (86400 * 30), "/"); setcookie ("fullname", $_POST["fullname"], time()+ (86400 * 30), "/"); setcookie ("fulladdress", $_POST["fulladdress"], time()+ (86400 * 30), "/"); setcookie ("emailaddress", $_POST["emailaddress"], time()+ (86400 * 30), "/"); setcookie ("phonenumber", $_POST["phoneumber"], time()+ (86400 * 30), "/"); setcookie ("username", $_POST["username"], time()+ (86400 * 30), "/"); setcookie ("password", $_POST["password"], time()+ (86400 * 30), "/"); setcookie ("project", $_POST["project"], time()+ (86400 * 30), "/"); setcookie ("subscription", $_POST["subscription"], time()+ (86400 * 30), "/"); setcookie ("gender", $_POST["radio"], time()+ (86400 * 30), "/"); } else { setcookie("image", ""); setcookie("fullname", ""); setcookie("fulladdress", ""); setcookie("emailaddress", ""); setcookie("phonenumber", ""); setcookie("username", ""); setcookie("password", ""); setcookie("project", ""); setcookie("subscription", ""); setcookie("gender", ""); } ?--> <!--php $servername = "localhost"; $username = "webdesign"; $password = "web@design&33"; $dbname = "webdesign"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn--->connect_error) { die("Connection failed: " . $conn->connect_error); } //Submit data if (isset($_POST["submitData"])) { $folder = "image/"; $image = $folder.basename($_FILES["file"]["name"]); if(empty($_POST["fullname"])) { echo "<script>alert('Please enter your full name.');</script>"; exit; }else{ $fullname = $_POST["fullname"]; } if(empty($_POST["fulladdress"])) { echo "<script>alert('Please enter your full address.');</script>"; exit; }else{ $fulladdress = $_POST["fulladdress"]; } if(empty($_POST["emailaddress"])) { echo "<script>alert('Please enter your email address.');</script>"; exit; }else{ $emailaddress = $_POST["emailaddress"]; } if(empty($_POST["phonenumber"])) { echo "<script>alert('Please enter your phonenumber.');</script>"; exit; }else{ $phonenumber = $_POST["phonenumber"]; } if(empty($_POST["username"])) { echo "<script>alert('Please enter your username.');</script>"; exit; }else{ $username = $_POST["username"]; } if(empty($_POST["password"])) { echo "<script>alert('Please enter your password.');</script>"; exit; }else{ $password = $_POST["password"]; } if(empty($_POST["project"])) { echo "<script>alert('Please select your project.');</script>"; exit; }else{ $project = $_POST["project"]; } if(empty($_POST["subscription"])) { echo "<script>alert('Please select your subscription.');</script>"; exit; }else{ $subscription = $_POST["subscription"]; } if(empty($_POST["radio"])) { echo "<script>alert('Please select your gender.');</script>"; exit; }else{ $gender = $_POST["radio"]; } $stmt = $conn->prepare("INSERT INTO tutorials (image, fullname, fulladdress, emailaddress, phonenumber, username, password, project, subscription, gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("ssssssssss", $image, $fullname, $fulladdress, $phonenumber, $emailaddress, $username, $password, $project, $subscription, $gender); $stmt->execute(); $stmt->close(); if (move_uploaded_file($_FILES["file"]["tmp_name"], $image)) { header("Location: success.php"); } } $conn->close(); ?>

Retrieve users details after filling the form

After the form fully open go on to fill that form, after filling the form click submit. Users will leave the signup page locate a new page indicating a successful signup along with the details they submitted. Those details and information store in cookie container during signup. Save the below code as success.php in same folder where you saved signup.php

<h2>Congratulations!</h2> <hr> <h3>Your registration was successful, you can now login with your:</h3> <p>Username and password</p> <h4>Below details were received and stored in our database. </h4> <p><!--php if(isset($_COOKIE['fullname'])) { echo $_COOKIE['image']; }?--></p> <p><!--php if(isset($_COOKIE['fullname'])) { echo $_COOKIE['fullname']; }?--></p> <p><!--php if(isset($_COOKIE['fulladdress'])) { echo $_COOKIE['fulladdress']; }?--></p> <p><!--php if(isset($_COOKIE['emailaddress'])) { echo $_COOKIE['emailaddress']; }?--></p> <p><!--php if(isset($_COOKIE['phonenumber'])) { echo $_COOKIE['phonenumber']; }?--></p> <p><!--php if(isset($_COOKIE['username'])) { echo $_COOKIE['username']; }?--></p> <p><!--php if(isset($_COOKIE['password'])) { echo $_COOKIE['password']; }?--></p> <p><!--php if(isset($_COOKIE['project'])) { echo $_COOKIE['project']; }?--></p> <p><!--php if(isset($_COOKIE['subscription'])) { echo $_COOKIE['subscription']; }?--></p> <p><!--php if(isset($_COOKIE['gender'])) { echo $_COOKIE['gender']; }?--></p> <p>If you still are unable to login, don't hesitate to contact us.</p> <p><a style="margin:0; padding:15px; background:#04AA6D; font-size:18px; font-weight:bold; text-align:center; display:block; border-radius:10px; text-decoration:none; color:white;" href="deletecookie.php">Go back to login</a></p>

Destroy user details stored in cookie

It will really impress visitors to see their details after signup, before visitors leave that page there is a clickable link that will destroy the details store in cookie container and redirect them to login page. Now copy the code and save it as deletecookie.php. Super cool!

<!--php setcookie("image", ""); setcookie("fullname", ""); setcookie("fulladdress", ""); setcookie("emailaddress", ""); setcookie("phonenumber", ""); setcookie("username", ""); setcookie("password", ""); setcookie("project", ""); setcookie("subscription", ""); setcookie("gender", ""); header("Location: index.php"); ?-->

Summary

If followed the above tutorials correctly it will teach you how you can implement cookie in your application. If the above code spot error, don't hesitate to get in touch. You can submit your question in commentbox below. Thank you for reading this articles.

Comments

Latest post

We will take a closer look on how to bind webpage in python programming language

Today we will take a closer look on how to run python in your webpage under windows operating system. Let's assume you have installed xampp in your windows operating system and have python program installed as well. If you have not done so, then go on to installed xampp in your computer together with python program. Remember to select software version compactible to your computer. When you are ready with these two software, then copy the code below: #!"C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\python.exe" print ("Content-Type: text/html\n") print (" ") print (" ") print (" ") print (" ") print (" Hello world! ") print (" Welcome to my first python webpage ") print (" ") print (" ") Save the above code as index.py in C:\xampp\htdocs the path should be: C:\xampp\htdocs\index.py On your browser address bar you can type http://localhost/index.py, ...

Our today post will focus on how to implement php class and method

A class is a container for objects, and object is items in class. Class is container that we can store all kinds of obejects, such as variable, function etc. Today we will implement class and objects , property, set, get and the rest. If you look at the php class below, this is exactly how to declare empty php class. Php class container with public objects, function set parameters and gets parameters. In php class below, we created two functions, one to set a value and one to get the value, pass the instance object and its value. Now you can copy the below code for your testing. name = $name; } function getname() { return $this->name; } } $username = new KcFuns(); $username->setname('Kenan Chiquado'); echo "Output:"; echo "Username is: ".$username->getname(); echo "----------------------------------"; ?> Php class several objects function and parameters In our second php class implementation, we will lo...

Today we will look into how to implement cUrl in php application

Passing json file via cUrl in php We will implement passing json file via cUrl and submit to next server. cUrl is all about special communication between two or more server. Such as server1.com sending data to server2.com. A lot of beginner had always ask endless question about what is cUrl and what cUrl actually does. A simple answer there is: cUrl is a special communication between two or more server. Now, if you look at json code below, it has been neatly written and indented and ready for testing. Launch your notepad copy the json data below and save it as data.json place it in a folder where you saved other php file. { "id": "1", "image": "image\/chiquado.jpg", "fullname": "Kenan Chiquado", "fulladdress": "Udah, Igbo-eze North LGA, Enugu state, Nigeria", "emailaddress": "2348130802790", "phonenumber": "presschiquado@gmail.com...

Our today post will focus on how to implement php hits counter

PHP counter step 1: Have you ever wonder how you can monitor, view or learn how many times your website is been processed by visitors. How many times your website is been browse by your visitors? If your answer is 'no', then it's time to learn all these. You can learn how many times browser processed your website by binding certain php script on top of your webpage. Now, if you look at our php script below, this script will help you count how many times your website was processed at any given moment. To apply our php script below, you can copy and paste this script on top of your webpage before html declaration. PHP counter step 2: Again, the below script, you can copy and put it in a separate file. All you need to do is check time to time and see the digits number your script return. That's exactly how many times your website was processed. Example of the counter: Hits counter While applying our tutorials, if our code spot error, don't hesit...

In today post we will focus on how to create php script that could upload picture to server

Before writing script to upload files to server, you have to check ensure that php file upload is set to 'On' in php.ini. Open C:\xampp\php\php.ini file locate the line that says upload_file = On and also change the php upload max setting, example: php_max_upload = 100m. Now save the file, restart the server and start writing php script to upload your file. Step 1: Below is the html code to implement our practice of file upload. You can write it manually or copy and paste. " enctype="multipart/form-data" method="POST"> Uploader Step 2: Php command to target the upload folder, php comman to get file name, php command to check upload status, and php command to validate file extension. Below is our sample code: Step 3: We will use php global object called $_POST to get the image object. And check if the file is image or not, if it's not image the upload status will be set to zero. Below is our sample code: if(isset(...