OOP cookiescript
Ik ben een cookie script aan het maken, oop aan het leren, nou is mijn vraag of dit zo goed is op de OOP manier en belangrijker wat kan er beter!
Graag hoor ik wat er beter kan en als je er wat aan hebt veel plezier ermee ;-)
ohja, <form> moet nog worden opgepimpt...
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
class Controller_CookieController
{
private $config;
private $_remoteip;
private $_datetime;
private $_cookie;
//checks dbase
private $_checkIp;
public function __construct($config)
{
$this->config = $config;
$this->setRemoteIp( $_SERVER['REMOTE_ADDR'] );
$this->setDateTime( date('Y-m-d H:i:s'));
//if cookie accepted not is set
if( !isset( $_COOKIE["cookiesAccepted"] ) ){
//if post cookie is set
if( $_POST['cookie'] ){
//dbase ip check
$this->setCheckIp( "$this->_remoteip" );
//set cookie
$this->setCookie( $_POST['cookie'] );
//check if ip exists
if( !empty( $this->_checkIp[0]['ip'] ) ){
//update record, ip was found in dbase and cookie wasn't set
$this->updateCookie( $this->_datetime , $this->_checkIp[0]['ip'] , $_POST['cookie'] , $this->_checkIp[0]['id'] );
}
else{
//insert new record
$this->insertCookie( $this->_datetime, $this->_remoteip, $_POST['cookie'] );
}
}
else{
$actual_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$form = '';
$form .= '
<form action="'.$actual_url.'" method="post">
Do you accept cookie<br/>
<button type="submit" name="cookie" value="yes">yes</button>
<button type="submit" name="cookie" value="no">no</button>
</form>
';
echo $form;
}
}
else{
echo 'cookie isset';
}
}
public function getRemoteIp() {
return $this->_remoteip;
}
public function setRemoteIp($ip) {
$this->_remoteip = $ip;
}
public function getDateTime() {
return $this->_datetime;
}
public function setDateTime($time) {
$this->_datetime = $time;
}
public function getCookie() {
return $this->_cookie;
}
public function setCookie($cookie) {
setcookie("cookiesAccepted","$cookie", time()+3600*24*366 , '/');
$this->_cookie = $cookie;
}
public function getCheckIp() {
return $this->_checkIp;
}
public function setCheckIp( $checkIp ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->Cookie("$checkIp");
$this->_checkIp = $tblCookie;
}
public function updateCookie( $date, $ip , $cookie , $id ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->updateCookie( $date, $ip , $cookie ,$id );
}
public function insertCookie( $date, $ip , $cookie ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->insertCookie( $date, $ip , $cookie );
}
}
[/code]
class Controller_CookieController
{
private $config;
private $_remoteip;
private $_datetime;
private $_cookie;
//checks dbase
private $_checkIp;
public function __construct($config)
{
$this->config = $config;
$this->setRemoteIp( $_SERVER['REMOTE_ADDR'] );
$this->setDateTime( date('Y-m-d H:i:s'));
//if cookie accepted not is set
if( !isset( $_COOKIE["cookiesAccepted"] ) ){
//if post cookie is set
if( $_POST['cookie'] ){
//dbase ip check
$this->setCheckIp( "$this->_remoteip" );
//set cookie
$this->setCookie( $_POST['cookie'] );
//check if ip exists
if( !empty( $this->_checkIp[0]['ip'] ) ){
//update record, ip was found in dbase and cookie wasn't set
$this->updateCookie( $this->_datetime , $this->_checkIp[0]['ip'] , $_POST['cookie'] , $this->_checkIp[0]['id'] );
}
else{
//insert new record
$this->insertCookie( $this->_datetime, $this->_remoteip, $_POST['cookie'] );
}
}
else{
$actual_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$form = '';
$form .= '
<form action="'.$actual_url.'" method="post">
Do you accept cookie<br/>
<button type="submit" name="cookie" value="yes">yes</button>
<button type="submit" name="cookie" value="no">no</button>
</form>
';
echo $form;
}
}
else{
echo 'cookie isset';
}
}
public function getRemoteIp() {
return $this->_remoteip;
}
public function setRemoteIp($ip) {
$this->_remoteip = $ip;
}
public function getDateTime() {
return $this->_datetime;
}
public function setDateTime($time) {
$this->_datetime = $time;
}
public function getCookie() {
return $this->_cookie;
}
public function setCookie($cookie) {
setcookie("cookiesAccepted","$cookie", time()+3600*24*366 , '/');
$this->_cookie = $cookie;
}
public function getCheckIp() {
return $this->_checkIp;
}
public function setCheckIp( $checkIp ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->Cookie("$checkIp");
$this->_checkIp = $tblCookie;
}
public function updateCookie( $date, $ip , $cookie , $id ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->updateCookie( $date, $ip , $cookie ,$id );
}
public function insertCookie( $date, $ip , $cookie ) {
$db = new Model_Connect( $this->config['db'] );
$tblCookie = new Model_Table_Cookie($db);
$tblCookie = $tblCookie->insertCookie( $date, $ip , $cookie );
}
}
[/code]
Gewijzigd op 03/05/2013 12:26:11 door Bas D L
Maar je weet dat HTML-output niet in een class hoort?
@Aar, yes i know ;-) Thanks!
- Beslis wanneer je een _ prefix doet en wanneer niet. Nu is het heel inconsistent. Ik zeg nooit.
- Gebruik nooit superglobals in je methods. Wie zegt dat ik de cookies wil opslaan in cookies en wie beweerd dat we een 'cookie' element in de post hebben?
- Dit lijkt me niet iets wat in een controller afgehandeld wordt.
- Schrijf voor de grap eens op wat deze klasse allemaal doet en bedenk dan dat elke klasse maar 1 ding mag doen...
Wat bedoel je met punt 2 tm 5?