Πρόβλημα με ελληνικούς χαρακτήρες σε php

Σε αυτή την περιοχή μπορείτε να βρείτε ή να αναζητήσετε πληροφορίες σχετικές με την PHP

Συντονιστές: WebDev Moderators, Super-Moderators, PHP Moderators

Απάντηση
constantinosalpha
Δημοσιεύσεις: 7
Εγγραφή: 14 Απρ 2009 20:00

Πρόβλημα με ελληνικούς χαρακτήρες σε php

Δημοσίευση από constantinosalpha » 03 Ιουν 2009 17:38

Παιδιά χαιρετώ το φόρουμ και ζητώ τα φώτα σας.

Βρήκα ένα script για ένα βιβλίο επισκεπτών (http://www.jemjabella.co.uk/scripts/bellabook) που θέλω να φτιάξω στο site μου και έχω το εξής πρόβλημα.

Όταν βάζω ελληνικούς χαρακτήρες στα πεδία (όνομα, comments), δεν μπορώ να κάνω submit. Τα βγάζει ως σύμβολα. Μπορείτε να δοκιμάσετε στο www.c-andronis.gr/gb/index.php όπου, όπως θα δείτε, έχω κάνει προσπάθειες αλλά επι ματαίω.

Δεν συνδέεται με βάση δεδομένων το guestbook οπότε δεν είναι πρόβλημα mysql. Επικοινώνησα με τον τύπο που έφτιαξε το script και μου είπε να βάλω 3 γραμμές κώδικα για να αναγνωρίζει utf-8 αλλά ούτε αυτό δεν έφτιαξε την κατάσταση.

Επισυνάπτω το config.php και το sign.php

Κώδικας: Επιλογή όλων

<?php
//-----------------------------------------------------------------------------
// BellaBook Copyright © Jem Turner 2004-2007,2008 unless otherwise noted
// http&#58;//www.jemjabella.co.uk/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. See README.txt
// or LICENSE.txt for more information.
//-----------------------------------------------------------------------------

require_once&#40;'prefs.php'&#41;;


define&#40;"ENTRIES", "entries.txt"&#41;;
define&#40;"TEMPENTRIES", "tempentries.txt"&#41;;
define&#40;"IPBLOCKLST", "iplist.txt"&#41;;
define&#40;"SPAMWDS", "spamwords.txt"&#41;;


function cleanUp&#40;$text&#41; &#123;
    $text = trim&#40;htmlentities&#40;strip_tags&#40;$text&#41;, ENT_NOQUOTES, UTF-8&#41;&#41;;
    return $text;
&#125;


// break big words every 50 characters for layout preservation.
function linebreaker&#40;$text&#41; &#123;
	$new_text = '';
	$text_1 = explode&#40;'>',$text&#41;;
	$sizeof = sizeof&#40;$text_1&#41;;
	for &#40;$i=0; $i<$sizeof; ++$i&#41; &#123;
		$text_2 = explode&#40;'<',$text_1&#91;$i&#93;&#41;;
		if &#40;!empty&#40;$text_2&#91;0&#93;&#41;&#41; &#123;
			$new_text .= preg_replace&#40;'#&#40;&#91;^\s .&#93;&#123;50&#125;&#41;#i', '\\1  ', $text_2&#91;0&#93;&#41;;
		&#125;
		if &#40;!empty&#40;$text_2&#91;1&#93;&#41;&#41; &#123;
			$new_text .= '<' . $text_2&#91;1&#93; . '>';   
		&#125;
	&#125;
	return $new_text;
&#125;

function doAdminHeader&#40;&#41; &#123;
	global $stylecolor;
?>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http&#58;//www.w3.org/TR/html4/strict.dtd">
	<html>
	<head>
		<title>BellaBook Control Panel</title>
		<link href="<?php echo $stylecolor; ?>-stylesheet.css" rel="stylesheet" type="text/css">
	</head>
	<body>

	<div id="container">
	<p id="topnav"><a href="index.php">View the Guestbook</a> &middot; <a href="admin.php">Admin Main</a> &middot; <a href="logout.php">Logout</a></p>
<?php
&#125;
function doAdminFooter&#40;&#41; &#123;
	echo "\r\n</div>\r\n</body>\r\n</html>";
&#125;


// fix the blank lines in iplist/badwords/entries files
function blanklinefix&#40;$inputfile&#41; &#123;
	ignore_user_abort&#40;true&#41;;
	$content = file&#40;$inputfile&#41;;

	if &#40;count&#40;$content&#41; > 0&#41; &#123;
		$content = array_diff&#40;array_diff&#40;$content, array&#40;""&#41;&#41;, array&#40;"\r\n"&#41;&#41;;

		$newContent = array&#40;&#41;;
		foreach &#40;$content as $line&#41; &#123;
			$newContent&#91;&#93; = trim&#40;$line&#41;;
		&#125;
		$newContent = implode&#40;"\r\n", $newContent&#41;;
	
		$fl = fopen&#40;$inputfile, "w+"&#41;;
		if &#40;flock&#40;$fl, LOCK_EX&#41;&#41; &#123;
			fwrite&#40;$fl, $newContent&#41;;
			flock&#40;$fl, LOCK_UN&#41;;
		&#125; else &#123;
			echo 'The file&#58; '.$inputfile.' could not be locked for writing; the blanklinefix function could not be applied at this time.';
		&#125;
		fclose&#40;$fl&#41;;
	&#125;
	ignore_user_abort&#40;false&#41;;
&#125;

function doWrite&#40;$file2open, $data, $writetype&#41; &#123;
	$file = fopen&#40;$file2open, $writetype&#41; or die&#40;"ERROR&#58; could not open ".$file2open&#41;;
	if &#40;flock&#40;$file, LOCK_EX&#41;&#41; &#123;
		fwrite&#40;$file, $data&#41;;
		flock&#40;$file, LOCK_UN&#41;;
	&#125; else &#123;
		exit&#40;"ERROR&#58; could not lock ".$file2open&#41;;
	&#125;
	fclose&#40;$file&#41;;
&#125;

function sign_gbook&#40;$file, $entry&#41; &#123;
	// one of these days I really should really should re-write the whole lot and just use sorting functions
	
	$oldData = file_get_contents&#40;$file&#41;;
	doWrite&#40;$file, $entry, "w"&#41;; // write the new data
	doWrite&#40;$file, $oldData, "a"&#41;; // append the old data

	echo "<p>Thank you for signing the guestbook.</p>";

	if &#40;$file === TEMPENTRIES&#41;
		echo "<p>Moderation is enabled, the guestbook owner will have to approve your message before it appears.</p>";
&#125;

function emoticonise&#40;$message&#41; &#123;
	global $smilies;
		$path_to_smilies = "smilies/";

	if &#40;isset&#40;$smilies&#41; && $smilies == "yes"&#41; &#123;
		$smiliesA = array&#40;
			'&#58;&#41;'      => 'smile.gif',
			'&#58;D'      => 'biggrin.gif',
			'&#58;&#40;'      => 'sad.gif',
			'&#58;P'      => 'tongue.gif',
			'&#58;o'      => 'shocked.gif',
			';&#41;'      => 'wink.gif',
		&#41;;
		foreach &#40;$smiliesA as $key => $value&#41; &#123;
			$message = str_replace&#40;$key, " <img src='".$path_to_smilies.$value."' alt='$key' title='$key' />", $message&#41;;
		&#125;
		return $message;
	&#125; else &#123;
		return $message;
	&#125;
&#125;

function checkBots&#40;&#41; &#123;
	$isbot = false;
	
	$bots = array&#40;"Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot"&#41;;
	foreach &#40;$bots as $bot&#41;
		if &#40;strpos&#40;$_SERVER&#91;'HTTP_USER_AGENT'&#93;, $bot&#41; !== false&#41;
			$isbot = true;

	if &#40;empty&#40;$_SERVER&#91;'HTTP_USER_AGENT'&#93;&#41; || $_SERVER&#91;'HTTP_USER_AGENT'&#93; == " "&#41;
		$isbot = true;
	
	if &#40;$isbot === true&#41; &#123;
		echo "<p>Safety checks indicate there's a high probability that you're a bot, and bots aren't allowed to submit links.</p>";
		exit&#40;include&#40;'footer.php'&#41;&#41;;
	&#125;
&#125;
function breakEmail&#40;$email&#41; &#123;
	$email = str_replace&#40;'.', 'DOTTY', $email&#41;;
	$email = str_replace&#40;'@', 'ATTIE', $email&#41;;
	$email = str_replace&#40;'-', 'DASHY', $email&#41;;
	$email = str_replace&#40;'_', 'SCORE', $email&#41;;

	return $email;
&#125;
function fixEmail&#40;$email&#41; &#123;
	$email = str_replace&#40;'DOTTY', '.', $email&#41;;
	$email = str_replace&#40;'ATTIE', '@', $email&#41;;
	$email = str_replace&#40;'DASHY', '-', $email&#41;;
	$email = str_replace&#40;'SCORE', '_', $email&#41;;

	return $email;
&#125;


if &#40;$emailrequired == "yes"&#41; $req = "<small>&#40;req.&#41;</small>"; else $req = "<small>&#40;not req.&#41;</small>";
if &#40;$showemail == "yes"&#41; $disp = " <small>&#40;displayed&#41;</small>"; else $disp = " <small>&#40;not shown&#41;</small>";

function countcontents&#40;$fileloc&#41; &#123;
	// why did I put this in? it's so redundant...
	if &#40;filesize&#40;$fileloc&#41; > 0&#41; return count&#40;file&#40;$fileloc&#41;&#41;;
	else return 0;
&#125;
blanklinefix&#40;ENTRIES&#41;;
blanklinefix&#40;TEMPENTRIES&#41;;
blanklinefix&#40;IPBLOCKLST&#41;;
blanklinefix&#40;SPAMWDS&#41;;

error_reporting&#40;0&#41;;
?>

Κώδικας: Επιλογή όλων

<?php
//-----------------------------------------------------------------------------
// BellaBook Copyright © Jem Turner 2004-2007,2008 unless otherwise noted
// http&#58;//www.jemjabella.co.uk/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. See README.txt
// or LICENSE.txt for more information.
//-----------------------------------------------------------------------------

$show_form = true;
$error_msg = NULL;

if &#40;isset&#40;$_POST&#91;'submit'&#93;&#41; || $_SERVER&#91;'REQUEST_METHOD'&#93; == "POST"&#41; &#123;
	require_once&#40;'config.php'&#41;;
	if &#40;isset&#40;$captcha&#41; && $captcha == "yes"&#41; &#123;
		session_start&#40;&#41;;
		if&#40;md5&#40;$_POST&#91;'captcha'&#93;&#41; != $_SESSION&#91;'key'&#93;&#41; &#123;
			setcookie&#40;session_name&#40;&#41;, '', time&#40;&#41;-36000, '/'&#41;;
			$_SESSION = array&#40;&#41;;
			session_destroy&#40;&#41;;

			include&#40;'header.php'&#41;;
			echo "<p>The text you entered didn't match the image, please <a href='sign.php'>try again</a>.</p>";
			include&#40;'footer.php'&#41;;
			exit;
		&#125;
		if &#40;isset&#40;$_SESSION&#91;'key'&#93;&#41; && isset&#40;$_COOKIE&#91;session_name&#40;&#41;&#93;&#41;&#41; &#123;
			setcookie&#40;session_name&#40;&#41;, '', time&#40;&#41;-36000, '/'&#41;;
			$_SESSION = array&#40;&#41;;
			session_destroy&#40;&#41;;
		&#125;
	&#125;
	include&#40;'header.php'&#41;;

	// let's do some pattern matching on the IP to make sure this visitor is legit, not banned and not flooding
	$ipPattern = '/\b&#40;25&#91;0-5&#93;|2&#91;0-4&#93;&#91;0-9&#93;|&#91;01&#93;?&#91;0-9&#93;&#91;0-9&#93;?&#41;\.&#40;25&#91;0-5&#93;|2&#91;0-4&#93;&#91;0-9&#93;|&#91;01&#93;?&#91;0-9&#93;&#91;0-9&#93;?&#41;\.&#40;25&#91;0-5&#93;|2&#91;0-4&#93;&#91;0-9&#93;|&#91;01&#93;?&#91;0-9&#93;&#91;0-9&#93;?&#41;\.&#40;25&#91;0-5&#93;|2&#91;0-4&#93;&#91;0-9&#93;|&#91;01&#93;?&#91;0-9&#93;&#91;0-9&#93;?&#41;\b/i';
	
	if &#40;filesize&#40;IPBLOCKLST&#41; > 0&#41; &#123;
		$BlockedIPs = file&#40;IPBLOCKLST&#41;;
		
		// loop through and trim, otherwise the IP filter doesn't work
		foreach&#40;$BlockedIPs as $key => $ip&#41;
			$BlockedIPs&#91;$key&#93; = trim&#40;$ip&#41;;
		
		$iplist = '/&#40;' . implode&#40;'|', $BlockedIPs&#41; . '&#41;/';
	&#125;

	if &#40;$floodcontrol == "yes" && filesize&#40;ENTRIES&#41; > 0&#41; &#123;
		$open2check = file&#40;ENTRIES&#41;;
		$expodelineone = explode&#40;",", $open2check&#91;'0'&#93;&#41;;
			if &#40;$_SERVER&#91;'REMOTE_ADDR'&#93; == $expodelineone&#91;'4'&#93;&#41;	&#123;
				echo "<p>Sorry, you can't sign the guestbook twice in a row.</p>";
				exit&#40;include&#40;'footer.php'&#41;&#41;;
			&#125;
	&#125;
		
	if &#40;!preg_match&#40;$ipPattern, $_SERVER&#91;'REMOTE_ADDR'&#93;&#41; || &#40;isset&#40;$iplist&#41; && preg_match&#40;$iplist, $_SERVER&#91;'REMOTE_ADDR'&#93;&#41;&#41;&#41; &#123;
		echo "<p>Your IP is not valid or it has been banned, you cannot sign the guestbook.</p>\n\n";
		exit&#40;include&#40;'footer.php'&#41;&#41;;
	&#125;

	// check to make sure it's not a known bot 
	checkBots&#40;&#41;;
	
	// check for links before we clean up so they don't get removed with strip_tags
	if &#40;isset&#40;$allowlinks&#41; && $allowlinks == "no" && &#40;substr_count&#40;$_POST&#91;'comments'&#93;, 'http&#58;//'&#41; > 0 || substr_count&#40;$_POST&#91;'comments'&#93;, 'URL='&#41; > 0&#41;&#41; &#123;
		echo "<p>Your message contains URLs. To cut down on spam, the posting of URLs/links has been disabled. \n</p>";
		exit&#40;include&#40;'footer.php'&#41;&#41;;
	&#125;
	
	// prepare spam words
	if &#40;filesize&#40;SPAMWDS&#41; > 0&#41; &#123;
		$spamlist = file&#40;SPAMWDS&#41;;
		
		// loop through and trim, otherwise the spam filter doesn't work
		foreach&#40;$spamlist as $key => $spamword&#41;
			$spamlist&#91;$key&#93; = trim&#40;$spamword&#41;;

		$SpamWords = '/&#40;' . implode&#40;'|', $spamlist&#41; . '&#41;/i';
	&#125;

	// check for javascript exploits/spam and clean up the data 
	$exploits = "/&#40;content-type|bcc&#58;|cc&#58;|document.cookie|onclick|onload|javascript|alert&#41;/i";
	foreach &#40;$_POST as $key => $val&#41; &#123;
		if &#40;isset&#40;$SpamWords&#41; && preg_match&#40;$SpamWords, urldecode&#40;$val&#41;&#41;&#41; &#123;
			echo "<p>Your message contains words in the spam list, please go back and remove references to obvious 'spam' material. \n</p>";
			exit&#40;include&#40;'footer.php'&#41;&#41;;
		&#125;
		if &#40;preg_match&#40;$exploits, $val&#41;&#41; &#123;
			echo "<p>No meta injection, please. \n</p>";
			exit&#40;include&#40;'footer.php'&#41;&#41;;
		&#125;
		
		$c&#91;$key&#93; = cleanUp&#40;$val&#41;;
	&#125;

	// do some final checks 
	$error_msg = NULL;
	if &#40;!empty&#40;$c&#91;'human'&#93;&#41;&#41; &#123;
		$error_msg .= "Spam detection tells me you're not human.";	
	&#125; elseif &#40;empty&#40;$c&#91;'name'&#93;&#41; || !ereg&#40;"^&#91;A-Za-z' -&#93;*$", $c&#91;'name'&#93;&#41; || strlen&#40;$c&#91;'name'&#93;&#41; > 12&#41; &#123;
		$error_msg .= "Name is a invalid&#58; must not be blank, must have no special characters, must not exceed 12 characters.";
	&#125; elseif &#40;$emailrequired == "yes" && empty&#40;$c&#91;'email'&#93;&#41;&#41; &#123;
		$error_msg .= "E-mail is a required field, please fill it in.";
	&#125; elseif &#40;!empty&#40;$c&#91;'email'&#93;&#41; && !ereg&#40;"^&#91;_a-z0-9-&#93;+&#40;\.&#91;_a-z0-9-&#93;+&#41;*@&#91;a-z0-9-&#93;+&#40;\.&#91;a-z0-9-&#93;+&#41;*&#40;\.&#91;a-z&#93;&#123;2,6&#125;&#41;$", strtolower&#40;$c&#91;'email'&#93;&#41;&#41;&#41; &#123;
		$error_msg .= "The e-mail address that you provided is not valid.";
	&#125; elseif &#40;&#40;!empty&#40;$c&#91;'url'&#93;&#41; && $c&#91;'url'&#93; != "http&#58;//"&#41; && !preg_match&#40;'/^&#40;http|https|ftp&#41;&#58;\/\/&#40;&#40;&#91;A-Z0-9&#93;&#91;A-Z0-9_-&#93;*&#41;&#40;\.&#91;A-Z0-9&#93;&#91;A-Z0-9_-&#93;*&#41;+&#41;&#40;&#58;&#40;\d+&#41;&#41;?\/?/i', $c&#91;'url'&#93;&#41;&#41; &#123;
		$error_msg .= "The website address that you provided is not valid.";
	&#125; elseif &#40;empty&#40;$c&#91;'comments'&#93;&#41; || strlen&#40;$c&#91;'comments'&#93;&#41; < 10&#41; &#123;
		$error_msg .= "Your comment is too short.";
	&#125;
	
	if &#40;$error_msg == NULL&#41; &#123;
		$show_form = false;

		// let's make the data look nice and pretty
		$c&#91;'name'&#93; = ucwords&#40;strtolower&#40;$c&#91;'name'&#93;&#41;&#41;;
		$c&#91;'email'&#93; = strtolower&#40;$c&#91;'email'&#93;&#41;;
		$c&#91;'comments'&#93; = str_replace&#40;"<br /><br /><br /><br />", "<br /><br />", preg_replace&#40;"/,&#40;?! &#41;/", ", ", preg_replace&#40;"&#40;&#91;\r\n&#93;&#41;", "<br />", $c&#91;'comments'&#93;&#41;&#41;&#41;;
		
		if &#40;$emailentries == "yes"&#41; &#123;
			$subject = "New entry in guestbook &#40;$title&#41;";

			$message  = "Name&#58; ".$c&#91;'name'&#93;." \r\n";
			$message .= "E-mail&#58; ".$c&#91;'email'&#93;." \r\n";
			$message .= "Website&#58; ".$c&#91;'url'&#93;." \r\n";
			$message .= "Comments&#58; ".$c&#91;'comments'&#93;." \r\n";

\r\n\r\n";
			$message .= "-- ADMIN INFO -- \r\n";
			$message .= "IP&#58; ".$_SERVER&#91;'REMOTE_ADDR'&#93;." \r\n";
			$message .= "Browser&#58; ".$_SERVER&#91;'HTTP_USER_AGENT'&#93;." \r\n";
			$message .= "Referrer&#58; ".$_SERVER&#91;'HTTP_REFERER'&#93;." \r\n";
			$message .= "Admin Panel&#58; ".$admin_gburl."/admin.php \r\n";

			if &#40;$moderate == "yes"&#41; $message .= "\r\nYou will need to approve this entry for it to appear in your guestbook.";

			$headers = "From&#58; ".$title." <$admin_email> \r\nReply-To&#58; <$email>";
			mail&#40;$admin_email,$subject,$message,$headers&#41;;
		&#125;

		$entryformat = $c&#91;'name'&#93;.",".breakEmail&#40;$c&#91;'email'&#93;&#41;.",".$c&#91;'url'&#93;.",".$_SERVER&#91;'REMOTE_ADDR'&#93;.',"'.$c&#91;'comments'&#93;.'"'."\r\n";

		if &#40;$moderate == "yes"&#41; sign_gbook&#40;TEMPENTRIES, $entryformat&#41;;
		else sign_gbook&#40;ENTRIES, $entryformat&#41;;
	&#125;
&#125;
if &#40;!isset&#40;$_POST&#91;'submit'&#93;&#41; || $show_form == true&#41; &#123;
	require_once&#40;'config.php'&#41;;
	include_once&#40;'header.php'&#41;;

	function get_data&#40;$var&#41; &#123;
		if &#40;isset&#40;$_POST&#91;$var&#93;&#41;&#41; echo cleanUp&#40;$_POST&#91;$var&#93;&#41;;
	&#125;
?>

<p>Fill in your details in the form below. No HTML allowed.</p>

<?php
	if &#40;$error_msg != NULL&#41; &#123;
		echo '<p><strong style="color&#58; red;">ERROR&#58;</strong><br />'.$error_msg.'</p>';
	&#125;
?>

<form action="sign.php" method="post">
<p class="hidden">
	<input type="checkbox" name="human" id="human" /> <label for="human">Leave this unticked if you're human &#58;&#41;</label>
</p>
<p>
	<input type="text" name="name" id="name" value="<?php get_data&#40;"name"&#41;; ?>" /> <label for="name">Name</label> <br />
	<input type="text" name="email" id="email" value="<?php get_data&#40;"email"&#41;; ?>" /> <label for="email">E-mail</label> <?php echo $req . $disp; ?><br />
	<input type="text" name="url" id="url" value="http&#58;//" /> <label for="url">Website URL</label> <br />
	<textarea name="comments" id="comments"><?php get_data&#40;"comments"&#41;; ?></textarea> <label for="comments">Comments</label> <br />
	<?php if &#40;isset&#40;$captcha&#41; && $captcha == "yes"&#41; &#123; ?>
	<img src="captcha.php" alt="" style="margin-bottom&#58; 2px;" /><br />
	<input type="text" name="captcha" id="captcha" /> <label for="captcha">Numbers in Image</label> <br />
	<?php &#125; ?>
	<input type="submit" id="submit" name="submit" value="Submit" />
</p>
</form>

<?php
&#125;
include&#40;'footer.php'&#41;; ?>
Ελπίζω να βρούμε κάποια λύση.

Ευχαριστώ εκ των προτέρων,

Κωνσταντίνος

Άβαταρ μέλους
Stadem
Δημοσιεύσεις: 198
Εγγραφή: 10 Μαρ 2007 20:45

Πρόβλημα με ελληνικούς χαρακτήρες σε php

Δημοσίευση από Stadem » 03 Ιουν 2009 22:00

μετά το

Κώδικας: Επιλογή όλων

<head>
Βάλε:

Κώδικας: Επιλογή όλων

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Αν συνεχίζει να σου παρουσιάζει πρόβλημα θα πρέπει να κάνεις το αρχείο σου utf8 without BOM μπορείς να κατεβάσεις το notepad ++ που το μετατρέπει.

Απάντηση

Επιστροφή στο “PHP Προγραμματισμός”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες