PHP: htmlspecialchars: "'" wordt "\'" grrrrrrr

Pagina: 1
Acties:

  • Victorio
  • Registratie: Januari 2001
  • Laatst online: 01-02-2024
Kword gek!
Heb dit php script voor de specialchars, tis voor een gastenboek, maar als iemand het woord 'foto's' wil posten staat er 'foto\'s'.
Dus lijkt het erop dat die 'word vervangen door \' maar waar beslist dit script dat?
Bij het gedeelte van de htmlspecialchars heb ik al heftig lopen kladderen, maar dit haalt niets uit. ;(
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?
// --------------------------------------------------------------- //
//
//    Verander de variabelen hieronder.

      $GastenboekDataBestand = "content/gastenboek.txt";
      $GastenboekIPBestand = "content/gastenboek_ip.txt";
      $PlaatjesPad = "images/";
      $Lettertype = "Arial,Helvetica";
      $Lettergrootte = "2";

//    Hierna hoef je niks meer te veranderen!
//
// --------------------------------------------------------------- //

echo("<HTML>\n<HEAD>\n<TITLE>Gastenboek</TITLE>\n</HEAD>\n<BODY>\n<CENTER>\n
<FONT face='$Lettertype' size=$Lettergrootte>\n\n");

include("$GastenboekDataBestand");

$datum1 = mktime(date("G"), date("i"), date("s"), date("n"), date("d"), date("Y")); 
$datum = (date("d-m-Y H:i:s", $datum1));
if($REMOTE_ADDR != "127.0.0.1") { $ipadres = $REMOTE_ADDR; } else { $ipadres = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]; }
$bestandje = file("$GastenboekIPBestand");
$lengte = count($bestandje);

if ($actie == "voegtoe") {

if ($naam == "") {
$ok = "nee"; 
$naamfout = "Je naam  is niet ingevuld!<BR>";
} 

if ($mail == "" || !eregi( "^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $mail)) { 
$ok = "nee";
$mailfout = "Je e-mail adres is incorrect!<BR>";
}

if ($woonplaats == "") { 
$ok = "nee";
$woonplaatsfout = "Je woonplaats is niet ingevuld!<BR>"; 
}

if ($bericht=="") {
$ok = "nee";
$berichtfout = " Het bericht is niet ingevuld!<BR>";
}

}

$form = "<form method=\"POST\" action=\"?pagina=gastenboek&amp;actie=voegtoe\">
<font face=\"Arial\" size=\"2\">
<b>$naamfout$mailfout$woonplaatsfout$berichtfout</b><BR>
<table align='center' border=0 cellpadding=0 callspacing=0>
<tr>
<td width=100 align='left' valign='middle'>Naam:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"naam\" class=\"form\" size=\"33\" value=\"$naam\"></td>
</tr>
<tr>
<td width=100 align='left' valign='middle'>E-mail:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"mail\" class=\"form\" size=\"33\" value=\"$mail\"></td>
</tr>
<tr>
<td width=100 align='left' valign='middle'>Woonplaats:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"woonplaats\" class=\"form\" size=\"33\" value=\"$woonplaats\"></td>
</tr>
<tr>
<td width=100 align='left' valign='top'>Bericht:</td><td width=150 align='left' valign='middle'><textarea rows=\"8\" name=\"bericht\" class=\"form\" cols=\"50\">$bericht</textarea></td>
</tr>
<tr>
<td></td>
<td align='left'>
<table align='center' border=0 cellpadding=0 callspacing=0><tr>
<td width=30 align='center'>[img]images/smilie_smile.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_wink.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_frown.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_sadley.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_sleephappy.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_confused.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_clown.gif[/img]</td>
</tr>
<tr>
<td width=30 align='center'>:a</td>
<td width=30 align='center'>:b</td>
<td width=30 align='center'>:c</td>
<td width=30 align='center'>:d</td>
<td width=30 align='center'>:e</td>
<td width=30 align='center'>:f</td>
<td width=30 align='center'>:g</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
<td align='left'>
<input type=\"submit\" class=\"formbutton\" value=\"Verzenden\">
</font>
</form>
</td>
</tr>
</table>
"; 

if ($actie == "toevoegen") {

for($i = 0; $i < $lengte; $i++) {
if(eregi($ipadres,$bestandje[$i])) { $Getekend = "Ja"; } 
else { }
}

if($Getekend == "Ja") {
echo("Je hebt deze maand al een bericht geplaatst!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
else {
echo $form;
}

}

elseif ($actie == "voegtoe") {

for($i = 0; $i < $lengte; $i++) {
if(eregi($ipadres,$bestandje[$i])) { $Getekend = "Ja"; } 
else { }
}

if($Getekend == "Ja") {
echo("Je hebt deze maand al een bericht geplaatst!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
else {

if ($ok == "nee") {
echo $form;
} 

else {
$naam = htmlspecialchars($naam);
$naam = wordwrap( $naam, 60, "<br>", 1); 
$woonplaats = wordwrap( $woonplaats, 60, "<br>", 1); 
$mail = wordwrap( $mail, 60, "<br>", 1); 
$bericht = htmlspecialchars($bericht); 
$bericht = ereg_replace("", "", $bericht); 
$bericht = ereg_replace("<", "&amp;lt;", $bericht); 
$bericht = ereg_replace(">", "&amp;gt;", $bericht); 
$bericht = ereg_replace("\:a", "[img]images/smilie_smile.gif[/img]", $bericht); 
$bericht = ereg_replace("\:b", "[img]images/smilie_wink.gif[/img]", $bericht); 
$bericht = ereg_replace("\:c", "[img]images/smilie_frown.gif[/img]", $bericht); 
$bericht = ereg_replace("\:d", "[img]images/smilie_sadley.gif[/img]", $bericht); 
$bericht = ereg_replace("\:e", "[img]images/smilie_sleephappy.gif[/img]", $bericht); 
$bericht = ereg_replace("\:f", "[img]images/smilie_confused.gif[/img]", $bericht); 
$bericht = ereg_replace("\:g", "[img]images/smilie_clown.gif[/img]", $bericht);
//$bericht = wordwrap( $bericht, 60, "<br>", 1); 
$bericht = nl2br($bericht);

$nieuw = "\n\n</center></b><b>Naam: </b>$naam<br>\n
<b>E-mail: </b><a href=mailto:$mail>$mail</a><br>\n
<b>Woonplaats: </b>$woonplaats</a><br>\n
<b>Geschreven op: </b>$datum<br>\n
<b>Bericht: </b><br>$bericht<br>\n
<hr>" .$berichten;

$GastenboekBestand = fopen("$GastenboekDataBestand","w");
fputs($GastenboekBestand,"<?");
fputs($GastenboekBestand,"\$berichten=\"$nieuw\"");
fputs($GastenboekBestand,"?>");
fclose($GastenboekBestand);

$ipfile = fopen("$GastenboekIPBestand", "a");
fputs($ipfile, "$ipadres\n");  
fclose($ipfile); 

echo("Het bericht is toegevoegd!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
}
}

else {
echo("<a href='?pagina=gastenboek&amp;actie=toevoegen'><span class=title>Teken het gastenboek</span></a><br><br><hr>\n
$berichten<br><a href='?pagina=gastenboek&amp;actie=toevoegen'><center><span class=title>Teken het gastenboek</span></a><br>\n");
}
?>

  • Nielsz
  • Registratie: Maart 2001
  • Niet online
Addslashes()
Stripslashes()

  • Victorio
  • Registratie: Januari 2001
  • Laatst online: 01-02-2024
Op dinsdag 16 oktober 2001 14:30 schreef Nielsz het volgende:
Addslashes()
Stripslashes()
Hmmmm, uitleg?

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

Op dinsdag 16 oktober 2001 14:31 schreef Victorio het volgende:

[..]

Hmmmm, uitleg?
www.php.net

Doet iets met Cloud (MS/IBM)


  • Willem
  • Registratie: Februari 2001
  • Laatst online: 17-09 19:55
offtopic:
* Willem is niet zo'n PHP guru maar
zou je niet eens je programmeer opmaak verbeteren ?
Dit is toch niet te lezen man |:(

Motor (of auto) onderhoud bijhouden


  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 22:25

Janoz

Moderator Devschuur®

!litemod

Op dinsdag 16 oktober 2001 14:31 schreef Victorio het volgende:

[..]

Hmmmm, uitleg?
addslashes()
stripslashes()

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

Op dinsdag 16 oktober 2001 14:32 schreef willem169 het volgende:
offtopic:
* D2k is niet zo'n PHP guru maar
zou je niet eens je programmeer opmaak verbeteren ?
Dit is toch niet te lezen man |:(
u vraagt wij draaien
[topic=152278/1/25]
mijn stukje daarin

Doet iets met Cloud (MS/IBM)


  • Banpei
  • Registratie: Juli 2001
  • Laatst online: 16-09 08:48
htmlspecialchars vervangt voor jouw alle characters die niet in html weergegeven worden, dus dat kan je niet beslissen... Hoewel je er wel iets van invloed op kan hebben. Zie ook de PHP manual

  • Victorio
  • Registratie: Januari 2001
  • Laatst online: 01-02-2024
Op dinsdag 16 oktober 2001 14:34 schreef Banpei het volgende:
htmlspecialchars vervangt voor jouw alle characters die niet in html weergegeven worden, dus dat kan je niet beslissen... Hoewel je er wel iets van invloed op kan hebben. Zie ook de PHP manual
Weet ik, heb ik gezien, maar dat werkt dus voor geen meter. En mijn vraag is waarom dat niet werkt.

  • chem
  • Registratie: Oktober 2000
  • Laatst online: 27-08 13:53

chem

Reist de wereld rond

ik word al helemaal eng als ik die code zie... wat een rotzooi en resource-vretende ding :'(

maar goed, htmlspecialchars plaatst GEEN backslashes, zoveel kan ik je wel vertellen :)

Klaar voor een nieuwe uitdaging.


  • Lurge
  • Registratie: Maart 2000
  • Niet online

Lurge

ActueleWind

met addslashes doe je bij elke ' en " en nog een aantal chars een \ plaatsen (dit is voor database/PHP nodig) en met stripslahes haal de die \ weer weg. Dat is de uitleg heel kort samengevat.

ActueleWind


Verwijderd

Het zou ook de automagische functie magic quotes van php wel eens kunnen zijn die je dwars zit:

http://www.php.net/manual/en/configuration.php#ini.magic-quotes-gpc

Drie opties (imho), er zijn er vast nog meer :-)

- stripslashes() op string loslaten die je naar je scherm stuurt;
- htmlspecialchars() uit laten voeren met ENT_QUOTES;
- magic quotes op je server uit zetten en lekker zelf een functietje bouwen om rare karakters te escapen

Hope this helps ;)

Verwijderd

Ik heb dat probleem ook eens gehad. Het aanmaken van de \ heeft niets te maken met htmlspecialchars, maar met het wegschrijven in de file. Op zich is dit een goede optie, als je er bij het ophalen van de strings maar rekening mee houdt dat de slashes ook weer verwijderd worden.

Zoals hieronder ;) :)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?
// --------------------------------------------------------------- //
//
//    Verander de variabelen hieronder.

      $GastenboekDataBestand = "content/gastenboek.txt";
      $GastenboekIPBestand = "content/gastenboek_ip.txt";
      $PlaatjesPad = "images/";
      $Lettertype = "Arial,Helvetica";
      $Lettergrootte = "2";

//    Hierna hoef je niks meer te veranderen!
//
// --------------------------------------------------------------- //

echo("<HTML>\n<HEAD>\n<TITLE>Gastenboek</TITLE>\n</HEAD>\n<BODY>\n<CENTER>\n
<FONT face='$Lettertype' size=$Lettergrootte>\n\n");

include("$GastenboekDataBestand");

$datum1 = mktime(date("G"), date("i"), date("s"), date("n"), date("d"), date("Y")); 
$datum = (date("d-m-Y H:i:s", $datum1));
if($REMOTE_ADDR != "127.0.0.1") { $ipadres = $REMOTE_ADDR; } else { $ipadres = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]; }
$bestandje = file("$GastenboekIPBestand");
$lengte = count($bestandje);

if ($actie == "voegtoe") {

if ($naam == "") {
$ok = "nee"; 
$naamfout = "Je naam  is niet ingevuld!<BR>";
} 

if ($mail == "" || !eregi( "^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $mail)) { 
$ok = "nee";
$mailfout = "Je e-mail adres is incorrect!<BR>";
}

if ($woonplaats == "") { 
$ok = "nee";
$woonplaatsfout = "Je woonplaats is niet ingevuld!<BR>"; 
}

if ($bericht=="") {
$ok = "nee";
$berichtfout = " Het bericht is niet ingevuld!<BR>";
}

}

$form = "<form method=\"POST\" action=\"?pagina=gastenboek&amp;amp;actie=voegtoe\">
<font face=\"Arial\" size=\"2\">
<b>$naamfout$mailfout$woonplaatsfout$berichtfout</b><BR>
<table align='center' border=0 cellpadding=0 callspacing=0>
<tr>
<td width=100 align='left' valign='middle'>Naam:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"naam\" class=\"form\" size=\"33\" value=\"$naam\"></td>
</tr>
<tr>
<td width=100 align='left' valign='middle'>E-mail:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"mail\" class=\"form\" size=\"33\" value=\"$mail\"></td>
</tr>
<tr>
<td width=100 align='left' valign='middle'>Woonplaats:</td><td width=150 align='left' valign='middle'><input type=\"text\" name=\"woonplaats\" class=\"form\" size=\"33\" value=\"$woonplaats\"></td>
</tr>
<tr>
<td width=100 align='left' valign='top'>Bericht:</td><td width=150 align='left' valign='middle'><textarea rows=\"8\" name=\"bericht\" class=\"form\" cols=\"50\">$bericht</textarea></td>
</tr>
<tr>
<td></td>
<td align='left'>
<table align='center' border=0 cellpadding=0 callspacing=0><tr>
<td width=30 align='center'>[img]images/smilie_smile.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_wink.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_frown.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_sadley.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_sleephappy.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_confused.gif[/img]</td>
<td width=30 align='center'>[img]images/smilie_clown.gif[/img]</td>
</tr>
<tr>
<td width=30 align='center'>:a</td>
<td width=30 align='center'>:b</td>
<td width=30 align='center'>:c</td>
<td width=30 align='center'>:d</td>
<td width=30 align='center'>:e</td>
<td width=30 align='center'>:f</td>
<td width=30 align='center'>:g</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
<td align='left'>
<input type=\"submit\" class=\"formbutton\" value=\"Verzenden\">
</font>
</form>
</td>
</tr>
</table>
"; 

if ($actie == "toevoegen") {

for($i = 0; $i < $lengte; $i++) {
if(eregi($ipadres,$bestandje[$i])) { $Getekend = "Ja"; } 
else { }
}

if($Getekend == "Ja") {
echo("Je hebt deze maand al een bericht geplaatst!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
else {
echo $form;
}

}

elseif ($actie == "voegtoe") {

for($i = 0; $i < $lengte; $i++) {
if(eregi($ipadres,$bestandje[$i])) { $Getekend = "Ja"; } 
else { }
}

if($Getekend == "Ja") {
echo("Je hebt deze maand al een bericht geplaatst!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
else {

if ($ok == "nee") {
echo $form;
} 

else {
$naam = htmlspecialchars($naam);
$naam = wordwrap( $naam, 60, "<br>", 1); 
$naam = stripslashes($naam);
$woonplaats = wordwrap( $woonplaats, 60, "<br>", 1);
$woonplaats = stripslashes($woonplaats);
$mail = wordwrap( $mail, 60, "<br>", 1); 
$bericht = htmlspecialchars($bericht); 
$bericht = ereg_replace("", "", $bericht); 
$bericht = ereg_replace("<", "&amp;amp;lt;", $bericht); 
$bericht = ereg_replace(">", "&amp;amp;gt;", $bericht); 
$bericht = ereg_replace("\:a", "[img]images/smilie_smile.gif[/img]", $bericht); 
$bericht = ereg_replace("\:b", "[img]images/smilie_wink.gif[/img]", $bericht); 
$bericht = ereg_replace("\:c", "[img]images/smilie_frown.gif[/img]", $bericht); 
$bericht = ereg_replace("\:d", "[img]images/smilie_sadley.gif[/img]", $bericht); 
$bericht = ereg_replace("\:e", "[img]images/smilie_sleephappy.gif[/img]", $bericht); 
$bericht = ereg_replace("\:f", "[img]images/smilie_confused.gif[/img]", $bericht); 
$bericht = ereg_replace("\:g", "[img]images/smilie_clown.gif[/img]", $bericht);
//$bericht = wordwrap( $bericht, 60, "<br>", 1); 
$bericht = nl2br($bericht);
$bericht = stripslashes($bericht);

$nieuw = "\n\n</center></b><b>Naam: </b>$naam<br>\n
<b>E-mail: </b><a href=mailto:$mail>$mail</a><br>\n
<b>Woonplaats: </b>$woonplaats</a><br>\n
<b>Geschreven op: </b>$datum<br>\n
<b>Bericht: </b><br>$bericht<br>\n
<hr>" .$berichten;

$GastenboekBestand = fopen("$GastenboekDataBestand","w");
fputs($GastenboekBestand,"<?");
fputs($GastenboekBestand,"\$berichten=\"$nieuw\"");
fputs($GastenboekBestand,"?>");
fclose($GastenboekBestand);

$ipfile = fopen("$GastenboekIPBestand", "a");
fputs($ipfile, "$ipadres\n");  
fclose($ipfile); 

echo("Het bericht is toegevoegd!<br>\n");
echo("Klik <a href='?pagina=gastenboek'>hier</a> om terug te gaan naar het gastenboek.\n");
}
}
}

else {
echo("<a href='?pagina=gastenboek&amp;amp;actie=toevoegen'><span class=title>Teken het gastenboek</span></a><br><br><hr>\n
$berichten<br><a href='?pagina=gastenboek&amp;amp;actie=toevoegen'><center><span class=title>Teken het gastenboek</span></a><br>\n");
}
?>

  • Lurge
  • Registratie: Maart 2000
  • Niet online

Lurge

ActueleWind

edit:

En toen had ik een dubbelpost op een of andere wazige menier

ActueleWind


  • Victorio
  • Registratie: Januari 2001
  • Laatst online: 01-02-2024
Thnx Lurge en Willy,
$bericht = stripslashes($bericht);
Xal dit zo eens ff proberen.
Pagina: 1