user1814946
user1814946

Reputation: 181

PHP cookies setting

I hate to say it but I have been working on what should have been a 30 minute assignment for a good 6 hours now with little to no progress. I am attempting to capture a name and email in a form, and set them to cookies that will last 10 minutes. While the cookies are active, the page should skip the form and just display the input. I have tried this with both cookies and sessions and cannot get it to work. At this point I have written and deleted at least a hundred lines of code and just can't really see what the problem is. This is my first time working with PHP. Any help would be appreciated. Currently this code creates the form, takes the info and posts it to the page correctly. When I go back to the page, it shows the form again. I assume this means the cookie isn't setting / sticking.

<?php 
 if (!empty($_POST)) {
  setcookie('Cname',$_POST['name'], time()+600);
  setcookie('Cemail', $_POST['email'], time()+600);
//  header("Location:HW2.php");
 } 

?>
<html>
<head>
<title> Assignment 2 Alcausin </title>
</head>
<body>

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$visibleForm = True;
if(isset($_COOKIE['name'])){
    $visibleForm = False;
}
if(isset($_POST['submit'])){
    $visibleForm = False;
    echo "Your Name: ";
    echo $_COOKIE['Cname'];
    echo "<br>";
    echo "Your Email: ";
    echo $_COOKIE['Cemail'];
}
if($visibleForm){ // close php if form is displayed
    ?>      
<form action ="HW2.php" method="post">
    Name:<font color = red>*</font> <input type="text" name="name"><br>
    E-mail:<font color = red>*</font> <input type="text" name="email"><br>
    <input type="submit" name="submit" value="Submit">
</form>
<?php   // back to php
}
?>

</body>
</html>

Upvotes: 0

Views: 146

Answers (2)

user2629998
user2629998

Reputation:

I rewrote your script using sessions, so that your data is actually stored on the server and the client only has a session cookie which is a reference to the server-side data, so the client has no way of tampering with that data.

While this may not be important for your homework, this is definitely important when you deal with user accounts and privileges (imagine an "admin" cookie that tells if the user is admin or not - anyone can manually set that cookie and that's it, he's an admin on your website).

This wasn't tested and may not work at all - feel free to downvote my answer if that's the case.

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set("session.cookie_lifetime","600"); // sets the session cookie's lifetime to 10 minutes / 600 seconds

session_start(); // starts the session, this will create a new session cookie on the client if there's not one already

if (isset($_POST["name"]) && isset($_POST["email"])) { // if there's POST data
    $_SESSION["name"] = $_POST["name"]; // this saves your values to the session so you can retrieve them later
    $_SESSION["email"] = $_POST["email"]; // same here
};

?>
<html>
<head>
<title> Assignment 2 Alcausin </title>
</head>
<body>
<?php

$visibleForm = !isset($_SESSION["name"]); // visibleForm will be the opposite of isset, so if there's a "name" in the session then the form will be invisible

if ($visibleForm) { // if there's no session data, we display the form
    echo '<form action ="HW2.php" method="post">Name:<font color = red>*</font> <input type="text" name="name"><br>E-mail:<font color = red>*</font> <input type="text" name="email"><br><input type="submit" name="submit" value="Submit"></form>';
} else { // this means there is some data in the session and we display that instead of the form
    echo "Your Name: ";
    echo $_SESSION["name"];
    echo "<br>";
    echo "Your Email: ";
    echo $_SESSION["email"];
};
?>

</body>
</html>

Upvotes: 1

remedy.
remedy.

Reputation: 2022

First of all, you must add the session_start() at the highest level of your code as it is essential for any of this to work. session_start() actually generates the PHPSESSID cookie and is also the session identifier; you won't need to set anything to the PHPSESSID cookie using setcookie() if you use session_start().

For a basic way to do what you're trying to achieve, I'd try to set sessions whenever the page loads and if there is a current session, then it will skip the form like you said.

$_SESSION['SESSID'] = $someVar;
$_SESSION['SESSNAME'] = "someOtherVar";

Then right before your form, check if any of those are set by using

if(isset($someVar) && isset($someOtherVar))

You know the deal.

Then create a button that does a session_destroy() so that it ends the current session.

Upvotes: 0

Related Questions