Script omzetten naar een functie
Goedenmiddag, ik wil graag dit script omzetten naar een function .. maar hoe kan ik dat het beste aanpakken?
Ik zit met het probleem dat ik gebruik maak van de functie: header .. hoe gaat dit in een functie? En hoe kan ik dit het beste aanpakken? Alle tips zijn welkom!
Bedankt alvast :-)
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
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
<?php
$naam_cookie = "cookies_enabled";
if (!isset($_COOKIE[$naam_cookie]) && !isset($_GET["toon_cookie_melding"])) {
if (isset($_GET["t_c"])) {
//cookies staan uit!
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\?t_c=1/", "", $url_balk);
}
elseif (preg_match("/\&t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\&t_c=1/", "", $url_balk);
}
if (preg_match("/\?/", $url_balk)) {
header("Location: ".$url_balk."&toon_cookie_melding=1");
}
else {
header("Location: ".$url_balk."?toon_cookie_melding=1");
}
}
else {
setcookie($naam_cookie, "1", 0, "/");
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $url_balk)) {
header("Location: ".$url_balk."&t_c=1");
}
else {
header("Location: ".$url_balk."?t_c=1");
}
}
}
if (isset($_GET["toon_cookie_melding"])) {
echo '<div style="padding: 5px; text-align: center; border-bottom: 1px solid #DB4343; background: #F05858;">';
echo 'Uw <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">cookies</a> staan uit.<br />';
echo 'U kunt <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">hier</a> meer info vinden om ze aan te zetten.';
echo '</div>';
}
if (isset($_COOKIE[$naam_cookie]) && (isset($_GET["t_c"]) && $_GET["t_c"] == 1)) {
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\?t_c=1/", "", $url_balk);
}
elseif (preg_match("/\&t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\&t_c=1/", "", $url_balk);
}
header("Location: ".$url_balk);
}
?>
$naam_cookie = "cookies_enabled";
if (!isset($_COOKIE[$naam_cookie]) && !isset($_GET["toon_cookie_melding"])) {
if (isset($_GET["t_c"])) {
//cookies staan uit!
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\?t_c=1/", "", $url_balk);
}
elseif (preg_match("/\&t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\&t_c=1/", "", $url_balk);
}
if (preg_match("/\?/", $url_balk)) {
header("Location: ".$url_balk."&toon_cookie_melding=1");
}
else {
header("Location: ".$url_balk."?toon_cookie_melding=1");
}
}
else {
setcookie($naam_cookie, "1", 0, "/");
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $url_balk)) {
header("Location: ".$url_balk."&t_c=1");
}
else {
header("Location: ".$url_balk."?t_c=1");
}
}
}
if (isset($_GET["toon_cookie_melding"])) {
echo '<div style="padding: 5px; text-align: center; border-bottom: 1px solid #DB4343; background: #F05858;">';
echo 'Uw <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">cookies</a> staan uit.<br />';
echo 'U kunt <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">hier</a> meer info vinden om ze aan te zetten.';
echo '</div>';
}
if (isset($_COOKIE[$naam_cookie]) && (isset($_GET["t_c"]) && $_GET["t_c"] == 1)) {
$url_balk = $_SERVER["REQUEST_URI"];
if (preg_match("/\?t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\?t_c=1/", "", $url_balk);
}
elseif (preg_match("/\&t_c=1/", $url_balk)) {
$url_balk = preg_replace("/\&t_c=1/", "", $url_balk);
}
header("Location: ".$url_balk);
}
?>
Ik zit met het probleem dat ik gebruik maak van de functie: header .. hoe gaat dit in een functie? En hoe kan ik dit het beste aanpakken? Alle tips zijn welkom!
Bedankt alvast :-)
Dit zijn leuke scriptjes om in OOP te schrijven (gewoon omdat het kan / leerzaam is), kijk eerst welke "methods" je kunt maken. Ik zie er zo al minstens 4
- newCookie
- setMessage
- getMessage
- setRedirect
Pak er een leuk tutorialtje bij en veel plezier!
http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php
- newCookie
- setMessage
- getMessage
- setRedirect
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
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
Pak er een leuk tutorialtje bij en veel plezier!
http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php
Gewijzigd op 08/02/2012 17:28:20 door Maikel Doeze
Precies ja! Dat soort kanten wil ik op! Heb alleen klein zetje in de rug nodig :-) maar, zo moet het wel goed komen denk ik.
Ik ga ff beetje rond neuzen en knutselen .. thnx voor je opzet! ;-)
Ik ga ff beetje rond neuzen en knutselen .. thnx voor je opzet! ;-)
Gewijzigd op 08/02/2012 17:23:13 door Joost van der Meijden
Geen probleem :)
Ow PS: setCookie gaat niet werken, #fail.. (en aangepast) newCookie? ook leuk ;)
Ow PS: setCookie gaat niet werken, #fail.. (en aangepast) newCookie? ook leuk ;)
Gewijzigd op 08/02/2012 17:26:42 door Maikel Doeze
@Maikel, Wanneer jij classes in jouw code gebruikt werk je niet gelijk in OOP.
Gewijzigd op 08/02/2012 17:43:42 door Joris van Rijn
@Joris,
Klopt, maar je moet als kennismaking ergens beginnen toch? ;)
Klopt, maar je moet als kennismaking ergens beginnen toch? ;)
Tip; gebruik sowieso geen 'var' meer maar fatsoenlijke 'modifiers' zoals public, private en protected. Dit geld zowel voor je operaties als attributen.
@Jaron
Je hebt gelijk, alleen misschien voor Joost al te diep in de materie? Vandaar linkje naar de tutorial.
Anyways, @Joost: Ik ben benieuwd naar het eindresultaat ;)
Je hebt gelijk, alleen misschien voor Joost al te diep in de materie? Vandaar linkje naar de tutorial.
Anyways, @Joost: Ik ben benieuwd naar het eindresultaat ;)
Gewijzigd op 08/02/2012 17:56:35 door Maikel Doeze
Ik heb nu dit als class:
En dit om de class aan te roepen:
Maar ik zie even door de bomen het bos niet meer .. gaat het wel de goede kant op?
----
edit:
Op deze manier moet de function newCookie eigenlijk in de construct, of zie ik dat verkeerd?
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
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
<?php
class check_cookie {
var $naam_cookie;
var $message;
function __construct($naam_cookie) {
$this->naam_cookie = $naam_cookie;
}
function newCookie() {
if (isset($_COOKIE[$this->naam_cookie])) {
$this->message = '1';
}
else {
setcookie($this->naam_cookie, "1", 0, "/");
$this->message = '0';
}
}
function setRedirect() {
if (!isset($_COOKIE[$this->naam_cookie]) && !isset($_GET["t_c"])) {
$location = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $location)) {
$location = $location."&t_c=1";
}
else {
$location = $location."?t_c=1";
}
$this->message = $location;
header("Location: ".$location);
}
if (!isset($_COOKIE[$this->naam_cookie]) && isset($_GET["t_c"])) {
$this->message = '2';
}
}
function getMessage() {
return $this->message;
}
/*
uitgangsmogelijkheden
0 geen cookie aangetroffen, cookie geset: redirecten.
1 cookie is bestaat (al), cookies staan dus aan.
2 cookie is geprobeert aan te maken, maar niet gelukt, cookies staan dus uit.
*/
}
?>
class check_cookie {
var $naam_cookie;
var $message;
function __construct($naam_cookie) {
$this->naam_cookie = $naam_cookie;
}
function newCookie() {
if (isset($_COOKIE[$this->naam_cookie])) {
$this->message = '1';
}
else {
setcookie($this->naam_cookie, "1", 0, "/");
$this->message = '0';
}
}
function setRedirect() {
if (!isset($_COOKIE[$this->naam_cookie]) && !isset($_GET["t_c"])) {
$location = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $location)) {
$location = $location."&t_c=1";
}
else {
$location = $location."?t_c=1";
}
$this->message = $location;
header("Location: ".$location);
}
if (!isset($_COOKIE[$this->naam_cookie]) && isset($_GET["t_c"])) {
$this->message = '2';
}
}
function getMessage() {
return $this->message;
}
/*
uitgangsmogelijkheden
0 geen cookie aangetroffen, cookie geset: redirecten.
1 cookie is bestaat (al), cookies staan dus aan.
2 cookie is geprobeert aan te maken, maar niet gelukt, cookies staan dus uit.
*/
}
?>
En dit om de class aan te roepen:
Code (php)
Maar ik zie even door de bomen het bos niet meer .. gaat het wel de goede kant op?
----
edit:
Op deze manier moet de function newCookie eigenlijk in de construct, of zie ik dat verkeerd?
Gewijzigd op 08/02/2012 18:45:37 door Joost van der Meijden
Begin al leuk te worden, je moet echter nog dieper met de stappen gaan, zie het echt als dingen die je moet "doen"/"stappen" (staat een prima tutorial op deze site met uitleg over OOP en de opbouw ervan).
Even een klein voorbeeldtje zodat je er weer op verder kunt borduren ;)
Even een klein voorbeeldtje zodat je er weer op verder kunt borduren ;)
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
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
<?php
class Cookie{
private $cookie;
private $message;
private $redirect;
private $location;
public function __construct($naam,$redirect = true)
{
$this->cookie = $naam; // Kookje naam natuurlijk defineren.
$this->redirect = $redirect; // Willen we uberhaupt wel redirecten?
}
public function newCookie()
{
if($this->checkCookie() === false){
return setcookie($this->cookie, "1", 0, "/");
} else {
return false;
}
}
public function getCookie()
{
if(isset($this->cookie)){
return $this->cookie;
} else {
return false;
}
}
public function checkCookie()
{
if(isset($_COOKIE[$this->cookie])){
return true;
} else {
return false;
}
}
public function setRedirect($location)
{
if(isset($location)){
$this->location = $location;
} else {
$this->location = false;
}
}
public function getRedirect()
{
if(isset($this->redirect)){
return $this->redirect;
} else {
return false;
}
}
public function runRedirect()
{
// Is redirect toegestaan?
if($this->redirect === true && isset($this->location)){
header("Location: ".$this->location);
exit;
} else {
// Dan doen we wat anders
return false;
}
}
}
$cookie = new Cookie('Kookje');
$cookie->setRedirect->('http://www.google.com');
if($cookie->newCookie())
{
$cookie->runRedirect();
}
?>
class Cookie{
private $cookie;
private $message;
private $redirect;
private $location;
public function __construct($naam,$redirect = true)
{
$this->cookie = $naam; // Kookje naam natuurlijk defineren.
$this->redirect = $redirect; // Willen we uberhaupt wel redirecten?
}
public function newCookie()
{
if($this->checkCookie() === false){
return setcookie($this->cookie, "1", 0, "/");
} else {
return false;
}
}
public function getCookie()
{
if(isset($this->cookie)){
return $this->cookie;
} else {
return false;
}
}
public function checkCookie()
{
if(isset($_COOKIE[$this->cookie])){
return true;
} else {
return false;
}
}
public function setRedirect($location)
{
if(isset($location)){
$this->location = $location;
} else {
$this->location = false;
}
}
public function getRedirect()
{
if(isset($this->redirect)){
return $this->redirect;
} else {
return false;
}
}
public function runRedirect()
{
// Is redirect toegestaan?
if($this->redirect === true && isset($this->location)){
header("Location: ".$this->location);
exit;
} else {
// Dan doen we wat anders
return false;
}
}
}
$cookie = new Cookie('Kookje');
$cookie->setRedirect->('http://www.google.com');
if($cookie->newCookie())
{
$cookie->runRedirect();
}
?>
Gewijzigd op 08/02/2012 19:47:57 door Maikel Doeze
Ik denk dat ik niet duidelijk genoeg was .. het was de bedoeling dat er een check was of cookies aan stonden. Ik heb het nu werkend en dit is mijn code:
Iemand nog op en/of aanmerkingen op mijn code?
Is het zo goed? Of hoe kan het beter?
Bedankt voor jullie hulp en tijd in ieder geval! :-)
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
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
<?php
class cookie {
private $naam_cookie;
private $location;
public $status;
public function __construct($naam_cookie) {
$this->naam_cookie = $naam_cookie;
if (isset($_COOKIE[$this->naam_cookie])) {
$this->status = '1';
if (preg_match("/t_c=1/", $_SERVER["REQUEST_URI"])) {
$loc = preg_replace("/\?t_c=1/", "", $_SERVER["REQUEST_URI"]);
$loc = preg_replace("/\&t_c=1/", "", $_SERVER["REQUEST_URI"]);
header("Location: ".$loc);
}
}
else {
if (isset($_GET["t_c"]) && !isset($_GET["cookie_melding"])) {
$this->status = '0';
header("Location: ".$this->getLocation("cookie_melding=1"));
}
elseif (isset($_GET["cookie_melding"])) {
$this->status = '0';
}
else {
$this->set_Cookie($this->naam_cookie);
}
}
}
private function getLocation($extra) {
if (isset($extra)) {
$location = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $location)) {
$location = $location."&".$extra;
}
else {
$location = $location."?".$extra;
}
}
else {
$location = $_SERVER["REQUEST_URI"];
}
return $location;
}
private function set_Cookie() {
setcookie($this->naam_cookie, "1", 0, "/");
header("Location: ".$this->getLocation("t_c=1"));
}
public function getStatus() {
return $this->status;
}
}
?>
class cookie {
private $naam_cookie;
private $location;
public $status;
public function __construct($naam_cookie) {
$this->naam_cookie = $naam_cookie;
if (isset($_COOKIE[$this->naam_cookie])) {
$this->status = '1';
if (preg_match("/t_c=1/", $_SERVER["REQUEST_URI"])) {
$loc = preg_replace("/\?t_c=1/", "", $_SERVER["REQUEST_URI"]);
$loc = preg_replace("/\&t_c=1/", "", $_SERVER["REQUEST_URI"]);
header("Location: ".$loc);
}
}
else {
if (isset($_GET["t_c"]) && !isset($_GET["cookie_melding"])) {
$this->status = '0';
header("Location: ".$this->getLocation("cookie_melding=1"));
}
elseif (isset($_GET["cookie_melding"])) {
$this->status = '0';
}
else {
$this->set_Cookie($this->naam_cookie);
}
}
}
private function getLocation($extra) {
if (isset($extra)) {
$location = $_SERVER["REQUEST_URI"];
if (preg_match("/\?/", $location)) {
$location = $location."&".$extra;
}
else {
$location = $location."?".$extra;
}
}
else {
$location = $_SERVER["REQUEST_URI"];
}
return $location;
}
private function set_Cookie() {
setcookie($this->naam_cookie, "1", 0, "/");
header("Location: ".$this->getLocation("t_c=1"));
}
public function getStatus() {
return $this->status;
}
}
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
include 'inc/cookie_check2.php';
$cookie_name = 'test_cookie_008';
$cookie = new cookie($cookie_name);
$cookie_enabled = $cookie->getStatus($cookie_name);
if ($cookie_enabled == 0) {
echo '<div style="padding: 5px; text-align: center; border-bottom: 1px solid #DB4343; background: #F05858;">';
echo 'Uw <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">cookies</a> staan uit.<br />';
echo 'U kunt <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">hier</a> meer info vinden om ze aan te zetten.';
echo '</div>';
}
?>
include 'inc/cookie_check2.php';
$cookie_name = 'test_cookie_008';
$cookie = new cookie($cookie_name);
$cookie_enabled = $cookie->getStatus($cookie_name);
if ($cookie_enabled == 0) {
echo '<div style="padding: 5px; text-align: center; border-bottom: 1px solid #DB4343; background: #F05858;">';
echo 'Uw <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">cookies</a> staan uit.<br />';
echo 'U kunt <a href="http://support.google.com/accounts/bin/answer.py?hl=nl&answer=61416" target="_blank" style="color: #F9F2F2;">hier</a> meer info vinden om ze aan te zetten.';
echo '</div>';
}
?>
Iemand nog op en/of aanmerkingen op mijn code?
Is het zo goed? Of hoe kan het beter?
Bedankt voor jullie hulp en tijd in ieder geval! :-)
Gewijzigd op 09/02/2012 21:31:10 door Joost van der Meijden
Classnamen kun je het best met een hoofdletter laten beginnen. En gebruik in je functie namen of CamelCase of under_scores, maar niet allebei (ik doel op set_Cookie) en gebruik ook of de een of de ander, dus niet de ene method CamelCase en de ander under_score.
Public gebruik je bijna nooit en ik zal in dit geval ook een getStatus method aanmaken en cookie::$status protected maken. Want het lijkt me niet de bedoeling dat je cookie::$status kan veranderen.
Ook zou ik alle private variabele veranderen naar protected. Je moet altijd rekening houden met uitbouwen.
$naam_cookie is een beetje overbodig lang. Het is de cookie class, dus als je $naam hebt is het de naam van de cookie.
Je doet nog steeds veel te veel dingen in 1 method. Kijk eens hoe Pim het oplost in deze tutorial. Hij werkt hier met sessions, maar bouw dat om naar cookies en je bent een goed eind op weg.
Verder over je code:
- getallen buiten quotes, niet '0' maar 0
- geen inline CSS gebruiken
- HTML echoën is totaal overbodig, sluit PHP en plaats daarin de HTML en open PHP daarna weer
Public gebruik je bijna nooit en ik zal in dit geval ook een getStatus method aanmaken en cookie::$status protected maken. Want het lijkt me niet de bedoeling dat je cookie::$status kan veranderen.
Ook zou ik alle private variabele veranderen naar protected. Je moet altijd rekening houden met uitbouwen.
$naam_cookie is een beetje overbodig lang. Het is de cookie class, dus als je $naam hebt is het de naam van de cookie.
Je doet nog steeds veel te veel dingen in 1 method. Kijk eens hoe Pim het oplost in deze tutorial. Hij werkt hier met sessions, maar bouw dat om naar cookies en je bent een goed eind op weg.
Verder over je code:
- getallen buiten quotes, niet '0' maar 0
- geen inline CSS gebruiken
- HTML echoën is totaal overbodig, sluit PHP en plaats daarin de HTML en open PHP daarna weer




