:::: MENU ::::
Browsing posts in: Articles

Wybór odpowiedniego kodu odpowiedzi HTTP – przestań utrudniać sobie życie!

Poniższy tekst jest mniej lub bardziej udanym tłumaczeniem artykułu Choosing an HTTP Status Code — Stop Making It Hard :).

Nie ma rzeczy prostszej niż zwrócenie kodu odpowiedzi HTTP. Strona się wyświetliła? Świetnie, zwróćmy kod 200. Strona nie istnieje? To kod 404. Przekierowujemy użytkownika na inną stronę? No to 302 albo 301.

Życie jest piękne dopóki ktoś powie Ci, że nie używasz REST-a. Nie możesz w nocy spać, bo zastanawiasz się, czy Twoja aplikacja zwraca zgodny z REST-em i zatwierdzony przez Roya Fieldinga kod odpowiedzi. Czy wystarczy 200? Czy powinien to być raczej 204 No Content? Nie, na pewno to powinien być 202 Accepted… a może jednak 201 Created?

To co komplikuje nam sytuację, to oficjalny standard HTTP/1.1, RFC, które pierwotnie powstało w 1997 roku (nie zawracaj sobie głowy RFC 2616, ani tym bardziej RFC 2068, zobacz RFC 7231). 1997 to rok, kiedy zacząłem przeglądać internet na Netscape Navigator i swoim modemie 33,6 kbps. To trochę jak używać Sztuki wojennej Sun Zi do współczesne strategii biznesowej. Nieśmiertelne rady, ale nie wyobrażam sobie jak wykorzystać pięć sposobów walki ogniem do testowania rynku.

Gdyby tylko istniał jakiś wizualny sposób na wybranie tylko tych istotnych kodów?

Proszę bardzo, internecie. Ten dzień nadszedł!

Continue Reading


Generating lottery codes – how many combinations will I have?

Here is a task. You have to prepare the schema for lottery codes. You have to generate lets say 1 000 000 codes that are 6 characters long. Client’s question is ‘Is that safe?’. This is a good question and you have to do some basic math to be sure that the combination of the code length and alphabet size give you enough combinations so users can’t guess other codes.

Simple example

Alphabet: A, B
Code length: 3 characters

So we have possible combinations:

AAA
AAB
ABA
ABB
BBB
BBA
BAA
BAB

So we have 8 possible combination for 3 characters code with 2 characters alphabet.

23 = 8

Later on, if we have 11 characters in our alphabet and 9 character code we will have

119 = 2 357 947 691

so if client wants 1 000 000 of codes, this scheme seems to be quite secure since is like 0,0424% of all possible combinations.


PHP foreach and passing by reference

$arr = array(1,2,3,4,5,6);

foreach($arr as $v){
    $arr[] = 1;
    echo $v;
}

output: 123456

$arr = array(1,2,3,4,5,6);

foreach($arr as &$v){
    $arr[] = 1;
    echo $v;
}

output: 1234561111111111111111111111111111111111111111111111…
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /Users/gradzinski/dev/playground/foreach.php on line 7

So within the foreach loop we are adding 1 to referenced array arr and this is why we are failing into infinite loop.

Below you can find PHP foreach manual explanation that may put some light in that behaviour.

Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won’t work:

<?php foreach (array(1, 2, 3, 4) as &$value) { $value = $value * 2; } ?>

PHP and passing object by reference

It is not really that we are passing object instances in PHP by reference. It is a little bit tricky, but the manual has this covered!

As of PHP 5, an object variable doesn’t contain the object
itself as value anymore. It only contains an object identifier which allows
object accessors to find the actual object. When an object is sent by
argument, returned or assigned to another variable, the different variables
are not aliases: they hold a copy of the identifier, which points to the same
object.

<?php 
class A { 
	public $foo = 1; 
} 

$a = new A; 
$b = $a; 
$a->foo = 2;
$a = NULL;
echo $b->foo."\n"; // 2

$c = new A;
$d = &$c;
$c->foo = 2;
$c = NULL;
echo $d->foo."\n"; // Notice:  Trying to get property of non-object…
?>