I have recently decided to go with MVC, and an object oriented appraoach using PHP.
I am trying to work with cookies. But i keep getting errors, i dont think i am executing the cookie properly…
Files used are here:-
Models/cookies.php
<?php
class Cookies{
public function setCookie($key, $value){
$value = $_POST['name'];
setcookie('firstname', $value, time()+ 3600);
return $return;
}
public function getCookie($key){
echo $_COOKIE['firstname'];
return $return;
}
public function deleteCookie($key){
setcookie('firstname', '');
return $return;
}
}
?>
Views/index.phtml
<html>
<head>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>';?>
<title>Cookies</title>
</head>
<body>
<div style="background: #c0c0c0; padding: 10px">
<?php if($cookiesPresent) : ?>
<p>Hello <?php echo $name; ?>, Welcome to this website.</p>
<p>The last time you visited this site was <?php echo $date; ?>.</p>
<?php else: ?>
<p>Welcome to this website. Please enter your name below.</p>
<?php endif; ?>
<form name="calc" action="?page=calc" method="post">
<table>
<tr><td>Name: </td><td><input type="text" name="name" /></td></tr>
<tr><td><input type="submit" name="submit" value="submit"/></td></tr>
</table>
</form>
</div>
</body>
</html>
Now i know i am doing this wrong, but please appreciate that there is a learning curve
these are the errors i get when i enter a name and click on the submit button:-
Notice: Undefined index: firstname in G:\xampp\htdocs\exercise6\index.php on line 10
Warning: Cannot modify header information - headers already sent by (output started at G:\xampp\htdocs\exercise6\index.php:1) in G:\xampp\htdocs\exercise6\index.php on line 12
Notice: Undefined index: firstname in G:\xampp\htdocs\exercise6\Models\cookies.php on line 18
Notice: Undefined variable: return in G:\xampp\htdocs\exercise6\Models\cookies.php on line 19
The notice is because you try to access $_COOKIE[‘firstname’] when it doesn’t exist. Nowhere do you create a cookie in your code, and you need to check if the cookie exists before you try to output it. It won’t exist until the next page load even if you create one during this execution.
The next error is because you generated a notice before you called header(), and you can’t send an HTTP header after the HTTP response body begins.
The next error once again corresponds to trying to read $_COOKIE[‘firstname’] when it doesn’t exist.
The next error refers to your “return $return” statement in the cookie class, when $return has never been defined or assigned a value.
Your setCookie method does not use $key and should not be accessing $_POST or hardcoding the cookie name. However, you never call it, so it doesn’t affect this code.
Your getCookie method does not return a cookie’s value as it probably is supposed to.
Your deleteCookie method does not use $key but instead hard codes the name as well.
The Cookie class as it is now has no purpose, as you’re not using it in any OO manner, it’s just a wrapper around several otherwise independent functions. Don’t create classes just for the sake of creating classes – but maybe you have something in mind you haven’t gotten to yet.
well this is the thing, i want to understand how classes and objects work in terms of Object Oriented coding PHP. I have currently coded procedurally, so i am trying to get to grips with the whole object oriented approach.
Also, is there some sort of tutorial that i could possibly follow to get me going?
How would i set a cookie and then check to see if it exists and output it?
Once i do this i can use this in my model. The reason why i am doing things this way is that i am trying to use the MVC framework so i am keeping everyting separate…
The thing you need to remember is that the cookie will not available until the next request. Therefore, what you can do is upon initiating the object store the cookie array and update that array in real time while also setting the actual cookie. This makes it possible to access cookie data that was set in the current request. Otherwise you would need to wait for the next request to actually access the information.
<?php
<?php
class Cookies {
private
$_arrCookies;
public function __construct() {
$this->_init();
}
private function _init() {
$this->_arrCookies = $_COOKIE;
}
public function setCookie($strName,$mixValue) {
setcookie($strName,$mixValue,time()+ 3600);
$this->_arrCookies[$strName] = $mixValue;
}
public function getCookie($strName) {
return isset($this->_arrCookies[$strName])?$this->_arrCookies[$strName]:null;
}
public function deleteCookie($strName) {
if(isset($this->_arrCookies[$strName])) {
setcookie($strName,'',time() - 3600);
unset($this->_arrCookies[$strName]);
}
}
}
$cookies = new Cookies();
$cookies->setCookie('firstname','tom');
echo '<pre>',print_r($_COOKIE),'</pre>';
echo '<p>',$cookies->getCookie('firstname'),'</p>';
?>