Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Kilka przydatnych rzeczy dla początkujących jak i zaawansowanych programistów.


Rekomendowane odpowiedzi

Opublikowano

Witam ! Dziś chcę dać wam 3 przydatne rzeczy przy tworzeniu gier via WWW

 

Numer 1 :

Funkcje

 

 

Jak wiadomo by mieć rekord z bazy trzeba pisać mysql_query potem mysql_fetch_array co jest męczące.

Dlatego stworzyłem przydatną funkcję - za 1 zamachem Wykona zapytanie i zwróci tablicę :) (mysql_fetch_array)

 

Zabezpieczenia... ehh trzeba się trudzić ,żeby wszystko było ok i sprawdzać pare razy :/. Właśnie dlatego stworzyłem funkcję :)

A oto one :

<?php
/*
Napisane przez Kongo dla MPC Forum.
Wszelkie prawa zastrzeżone.
Na MPC Forum Kongo używa loginu bnmbb.
Kongo 2011-2012.
www.worldofvillages.tk
www.mt2s.host22.com
http://eso.freeiz.com - już niedługo 

Możliwość rozpowszechniania TYLKO wtedy gdy na dole postu będzie widoczny napis :
Autorem funkcji jest Kongo.

Tylko do użytku niekomercyjnego.

[email protected]
*/
class Security {
//Prosta funkcja - a zabezpiecza ,że ja pier 
/*
Zabezpiecza przed Injectem. $veriable - zmienna która przesle.
$toINT - opcjonalnie. Zamienia zmienną na liczbę
Domyslnie FALSE
*/
public function InjectShield($veriable, $toINT = false) {
//Pierwsze - escape_string
mysql_real_escape_string($veriable);
//Znaki HTML
html_special_chars($veriable);
//Strip_tags
strip_tags($veriable);
//Jak by na bank dawał Inject'a 
add_slashes($veriable);

//Teraz opcjonalne $toINT
if($toINT) {
$veriable = (int)$veriable;
}
//Zwracamy wartosc :
return $veriable;
}
/* --------------------------------------------------------- >>>
 public System.Collections.Generic.IEnumerable<dynamic> Query(string commandText, params object[] parameters)
  Member of WebMatrix.Data.Database

 Summary:
 Queries the database using a SQL statement (optionally passing parameters) and returns the results.

 Parameters:
 commandText: The SQL statement to execute.
 parameters: (Optional) Parameters to pass to the SQL statement.

 Returns:
 The results of the query.

 # For SELECT, SHOW, DESCRIBE, EXPLAIN returns a resource on success, or FALSE on error.
 # return data array for dealing with result tables, to access the returned data.

Funkcja NIE została napisana przez Kongo lecz przez jego Ojca 

------------- */
 public function Query( $commandText, $parameters = null  )
 {
$arg_list = func_get_args();
$query = empty($parameters) ? $commandText : vsprintf($commandText, array_slice($arg_list, 1));

$result = mysql_query( $query );

if ( gettype($result)=='boolean' ) return $result;
 if ( mysql_num_rows($result)>0 ) {
  $qry_db = mysql_fetch_array( $result, MYSQL_ASSOC );
  mysql_free_result($result);
  return $qry_db;
}
//return;
 }

}
$security = new Security();
?>

i jak wywołać odpowiednią funkcję :

 

Dla zabezpieczeń :

$security->InjectShield($zmienna, $toINT=true); - jeśli mamy liczbę

$security->InjectShield($zmienna); - jeśli może jej nie być.

 

Dla zapytania :

$security->Query("Moje zapytanie...");

 

Mam nadzieję ,że się przyda w tworzeniu gier :)

 

 

 

2. Ścieżka absolutna w dołączeniu pliku :

 

 

Otóż jak spróbujemy napisać np. :

require_once("http://mpcforum.pl/test.php");

Wyskoczy nam coś takiego :
[code]
[b]Warning[/b]: require_once() [[url="http://worldofvillages.tk/function.require-once"]function.require-once[/url]]: URL file-access is disabled in the server configuration in [b]/home/kongo96lbn/domains/[/b]mpcforum.pl[b]/public_html/test.php[/b] on line [b]2[/b]

[b]Warning[/b]: require_once(http://mpcforum.pl/Nowy1.php) [[url="http://worldofvillages.tk/function.require-once"]function.require-once[/url]]: failed to open stream: no suitable wrapper could be found in [b]/home/kongo96lbn/domains/[/b]mpcforum.pl[b]/public_html/test.php[/b] on line [b]2[/b]

[b]Fatal error[/b]: require_once() [[url="http://worldofvillages.tk/function.require"]function.require[/url]]: Failed opening required 'http://mpcforum.pl/Nowy1.php' (include_path='.:/usr/local/lib/php') in [b]/home/kongo96lbn/domains/[/b]mpcforum.pl[b]/public_html/test.php[/b] on line [b]2[/b]

No i kurcze leżymy :(. Właśnie nie ! :)

Do tego wystarczy jedna linijka :

$absolute = $_SERVER['DOCUMENT_ROOT'];

I teraz znów chcemy dołączyć plik test.php ,który jest w głównym katalogu :

 

require_once($absolute."/test.php");

 

I kaboom - dołączyło :)

 

 

 

Numer 3 - CAPCHA

 

 

Czy trzeba tłumaczyć ?

CAPCHA z literami i cyframi. Wielkość znaków bez znaczenia :)

 

Oto kod :

<?php
#!/usr/bin/php_once
//<!--// PHP Script Document -------------------------------------------------//
session_start();
/* --------------------------------------------------------- >>>------------- */
class ReCaptcha
{
 public function GetHtml( $public_key, $theme = 'white' )
 {
//"PUBLIC_KEY", theme: "white"  array('q','g','q','g');//
$_key = self::Validate( $public_key );
$_SESSION['PUBLIC_KEY'] = implode( '', $_key );

## Create a new palette based image
$x_size = 116;
$y_size = 22;
$fontSize = 14;
$maxAngle = 20;

$x = 10;
$y = 16;

$image = imagecreate ( $x_size, $y_size );

## Allocate a color for an image
$black	= imagecolorallocate($image, 0x00, 0x00, 0x00);
$blue	 = imagecolorallocate($image, 0x00, 0x00, 0xFF);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$darkred  = imagecolorallocate($image, 0x90, 0x00, 0x00);
$gray	 = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$lime	 = imagecolorallocate($image, 0x00, 0xFF, 0x00);
$navy	 = imagecolorallocate($image, 0x00, 0x00, 0x80);
$red	  = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$white	= imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$yellow   = imagecolorallocate($image, 0xFF, 0xFF, 0x00);

if($theme=='white') $color = array( $red, $black, $blue, $darknavy, $darkred );
elseif($theme=='black') $color = array( $red, $white, $yellow, $lime );
else  $color = array( $red, $black, $blue, $darknavy, $darkred );

$bg1 = imagecolorallocate($image, 0x5C, 0x87, 0xB2);
$bg2 = imagecolorallocate($image, 0x00, 0x00, 0x00);
$bg3 = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$bg4 = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$bgc = array( 'blue'=>$bg1, 'black'=>$bg2, 'white'=>$bg3, 'red'=>$bg4);

## Flood fill
imagefill( $image, 0, 0, $bgc[$theme] );

for ($i=1; $i<=100; $i++) {
  imagesetpixel( $image, rand()%$x_size, rand()%$y_size, imagecolorallocate($image,rand()%255,rand()%255,rand()%255) );
}
for ($i=1; $i<=5; $i++) {
  $angle = (float)mt_rand(360-$maxAngle, 360+$maxAngle);
  if($theme=='white') $arccol = imagecolorallocate($image,rand(128,255),rand(128,255),rand(128,255));
  elseif($theme=='black') $arccol = imagecolorallocate($image,rand(0,128),rand(0,128),rand(0,128));
  else $arccol = imagecolorallocate($image,rand(128,255),rand(128,255),rand(128,255));

  imagearc( $image, rand()%$x_size, rand()%$y_size, 20, 20, 0, $angle, $arccol );
}

$dirName = str_replace('\\\\','/',dirname(__FILE__));

## Write text to the image using TrueType fonts
imagettftext( $image, $fontSize, (float)mt_rand(360-$maxAngle, 360+$maxAngle), $x, $y, $color[rand()%count($color)], $dirName."/arial.ttf", $_key[0] );
imagettftext( $image, $fontSize, (float)mt_rand(360-$maxAngle, 360+$maxAngle), ($x+20), $y, $color[rand()%count($color)], $dirName."/arial.ttf", $_key[1] );
imagettftext( $image, $fontSize, (float)mt_rand(360-$maxAngle, 360+$maxAngle), ($x+40), $y, $color[rand()%count($color)], $dirName."/arial.ttf", $_key[2] );
imagettftext( $image, $fontSize, (float)mt_rand(360-$maxAngle, 360+$maxAngle), ($x+60), $y, $color[rand()%count($color)], $dirName."/arial.ttf", $_key[3] );
imagettftext( $image, $fontSize, (float)mt_rand(360-$maxAngle, 360+$maxAngle), ($x+80), $y, $color[rand()%count($color)], $dirName."/arial.ttf", $_key[4] );
/*imagestring($image, 5, $x, 0, implode( ' ', $_key ), $color['red']);*/
## Output image to browser
$gen_key = imagejpeg($image, '', 100);
## Frees any memory associated with image
imagedestroy($image);

return $gen_key;
//return '<img src="$gen_key" border="0" alt="">';
 }

 private function RandomString( $size=4, $upper=TRUE, $lower=FALSE, $digit=TRUE, $chars=FALSE )
 {
$string = 0;
$randId = NULL;
$chrArr = array( '!', '"', '#', '$', '%', '&', '(', ')',
				 '*', '+', ',', '-', '.', '/', ':', ';',
				 '?', '@', '^', '_', '{', '|', '}' );

$countChr = count( $chrArr );
mt_srand ((double) microtime() * 1000000);
while( $string < $size ) {
  $random = mt_rand( 1, 4 );
  if( $upper==1 && $random==1) { $randId .= chr( mt_rand( 65, 90 )); }
  if( $digit==1 && $random==2) { $randId .= mt_rand( 1, 9 ); }
  if( $chars==1 && $random==3) { $randId .= $chrArr[mt_rand( 0, $countChr )]; }
  if( $lower==1 && $random==4) { $randId .= chr( mt_rand( 97, 122 )); }
  $string = strlen( $randId );
}
return $randId;
 }

 public function Validate( $private_key )
 {
 //"PRIVATE_KEY"ReCaptcha->Validate("PRIVATE_KEY")
$_key = array(
  self::RandomString(1,TRUE,TRUE),
  self::RandomString(1,TRUE,TRUE),
  self::RandomString(1,TRUE,TRUE),
  self::RandomString(1,TRUE,TRUE),
  self::RandomString(1,TRUE,TRUE));
$_SESSION['PRIVATE_KEY'] = implode('',$_key);
return $_key;
 }
}
/* --------------------------------------------------------- >>>------------- */
 $UrlData = null;
 if (isset($_SERVER['PATH_INFO'])) $UrlData = array_slice( explode('/', $_SERVER['PATH_INFO']), 1);
 else $UrlData[0] = 'white';
## Send a ReCaptcha raw image HTTP header
 header("Content-type: image/jpeg");
 ReCaptcha::GetHtml("PUBLIC_KEY", $UrlData[0]);
/* --------------------------------------------------------- >>>------------- */
?>

Tylko jak to umieścić ? Ano w formularzu dajemy :


		  <label for="captcha"><tr><td>Kod z obrazka :</label>
		  <td><input type="text" id="captcha" name="captcha" title="Confirm captcha" placeholder="Captcha key" />
		  <img src="Położenie CAPCHA" alt="<?php echo $_SESSION['PUBLIC_KEY'];?>" >

A w miejscu gdzie już rejestruje (tj. nie w formularzu tylko w skrypcie PHP)

Trzeba dać kod :

 

	if( strcmp( strtolower( $_REQUEST["captcha"] ), strtolower( $_SESSION['PRIVATE_KEY'] ) )<>0 ) {
		echo "Kod CAPTCHA niejest poprawny.";

	}

Uwaga ! Nazwa pola teksowego musi być captcha !

 

 

 

Mam nadzieję ,że komuś pomoglem.

 

 

 

 

Zakaz rozpowszechniania 3 części postu ! (Czyli CAPCHA !)

Opublikowano

mam pytanie dotyczące captcha trzeba zrobić oddzielny plik z pierwszysm spoilerem a 2 nastepne przy rejestracji ??

Opublikowano

Spoko poradnik przydatny, oczywiscie lajk leci.

 

PS. Tego szukałem :D

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...