Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Generowanie tokena
sajborg
post 4.12.2005, 11:18:31
Post #1





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


Witam mam taki sobie kodzik na token

token.php
  1. <?php
  2. $img["width"] = 240;
  3. $img["height"] = 86;
  4. $img["text"] = $_GET['t'];
  5. $img["file"] = @imagecreatefromjpeg ("token_background.jpg");
  6.  
  7. $font["angle"] = rand(-5,5);
  8. $font["files"] = array ( "stilltim.ttf", "dipedm.ttf", "zoinkfat.ttf" );
  9. $font["rand"] = array_rand ( $font["files"] );
  10. $font["file"] = $font["files"][$font["rand"]];
  11.  
  12. $token = imagecreatetruecolor ( $img["width"], $img["height"] );
  13.  
  14. $color["white"] = imagecolorallocate ( $token, 0,0,);
  15.  
  16. imagecopy ( $token, $img["file"], 0, 0, 0, 0, $img["width"], $img["height"] );
  17.  
  18. $box = imagettfbbox ( 24, $font["angle"], $font["file"], $img["text"] );
  19.  
  20. $x = (int) ( ( $img["width"] - abs ( max ( $box[2], $box[4] ) ) ) / 2);
  21. $y = (int) ( ( $img["height"] + abs ( max ( $box[5], $box[7] ) ) ) / 2);
  22.  
  23. imagecolorallocate ( $token, 0, 0, 0 );
  24. imagettftext ( $token, 24, $font["angle"], $x, $y, $color["white"], $font["file"], $img["text"] );
  25.  
  26. header ( 'Content-type: image/gif' );
  27. imagegif ( $token );
  28. imagedestroy( $token );
  29. ?>


Teraz w pliku z formem mam

register.php
  1. <?php
  2. $banned = array("#0#si", "#O#si");
  3. $tekst = preg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  4.  
  5. echo '<img src="token.php?t='.$tekst.'" alt="">';
  6.  
  7. ?>


Ale takie rozwiazanie latwo ominac bo kod tokena bedzie w kodzie a to roboty moga wychwycic. A Z kolei w pliku token.php nie moge generowac kodu bo bedzie mi on potrzebny w pliku form.php wiec jak mam to rozwiazac questionmark.gif?


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
mike
post 4.12.2005, 12:07:10
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


W pliku z tokenem jak najbardziej możesz generować token (hehe, jak to brzmi winksmiley.jpg )
A żeby cała reszta wiedziała co zostało wygenerowane i mogła sprzwdzić usera co wpisał musisz użyć ... sesji
Generujesz token ->
zapisaujesz go w sesji ->
pokazujesz na stronie ->
jak user coś wpisze to sprawdzasz z tym co masz w sesji
Go to the top of the page
+Quote Post
sajborg
post 4.12.2005, 12:08:03
Post #3





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


Heh zapomnialem o sesjach tongue.gifP THX


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
wibe
post 4.12.2005, 12:35:25
Post #4





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 26.11.2005

Ostrzeżenie: (0%)
-----


Tez kiedyś głowiłem się nad tokenem z generowanym kodem i zrobiłem to na bazie danych (mniej więcej tak jak to proponowali kiedyś na webhelpie).

Funckja genenruje mi obrazek i w tym czasie generowane jest ID kodu, zapisuje mi też czas tokena i ip.
Wszystkie funkcje losujace kod sa w obrazku ktory jest poprostu generowany (jak wczesniej napisałem podajemy mu tylko ID kodu).
Obrazek od razu zapisuje mi do bazy danych wszystkie informacje: ID kodu, kod ip usera i czas.

Następna funkcja ma zadanie sprawdzić czy kod jest poprawny jesli tak to zwraca TRUE. Nie zrobiłem w tej funkcji usuwania kodu jeśli jest poprawny, w wielu przypadkach gdy formularz jest rozbudowany, ktoś wpisze poprawnie kod ale inne pola błędnie a wtedy gdy da cofnij to przy następnym wypełnieniu już by kodu nei było. Dlatego dodałem funckję która usuwa mi użytego tokena oraz wszystkie kody ktore powstały więcej niż godzine temu.

Jak chcesz moge wkleić tutaj kod

Pozdrawiam.


--------------------
Go to the top of the page
+Quote Post
niko
post 4.12.2005, 13:06:26
Post #5





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


cześć ja mam puki co problem z tymi sesjami sad.gif

<b>mike_mech</b> możesz dopisać do kodu <b>paweb</b>
to jak się wrzuca ten wynik do sesji i potem jak sie go porównuje ?

pliiss smile.gif
Go to the top of the page
+Quote Post
sajborg
post 4.12.2005, 22:56:09
Post #6





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


token.php
  1. <?php
  2. $img["width"] = 240;
  3. $img["height"] = 86;
  4. $img["banned"] = array("#0#si", "#O#si");
  5. $img["text"] = reg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  6. $_SESSION["tokenid"]=$img["text"];
  7. $img["file"] = @imagecreatefromjpeg ("token_background.jpg");
  8.  
  9. $font["angle"] = rand(-5,5);
  10. $font["files"] = array ( "stilltim.ttf", "dipedm.ttf", "zoinkfat.ttf" );
  11. $font["rand"] = array_rand ( $font["files"] );
  12. $font["file"] = $font["files"][$font["rand"]];
  13.  
  14. $token = imagecreatetruecolor ( $img["width"], $img["height"] );
  15.  
  16. $color["white"] = imagecolorallocate ( $token, 0,0,);
  17.  
  18. imagecopy ( $token, $img["file"], 0, 0, 0, 0, $img["width"], $img["height"] );
  19.  
  20. $box = imagettfbbox ( 24, $font["angle"], $font["file"], $img["text"] );
  21.  
  22. $x = (int) ( ( $img["width"] - abs ( max ( $box[2], $box[4] ) ) ) / 2);
  23. $y = (int) ( ( $img["height"] + abs ( max ( $box[5], $box[7] ) ) ) / 2);
  24.  
  25. imagecolorallocate ( $token, 0, 0, 0 );
  26. imagettftext ( $token, 24, $font["angle"], $x, $y, $color["white"], $font["file"], $img["text"] );
  27.  
  28. header ( 'Content-type: image/gif' );
  29. imagegif ( $token );
  30. imagedestroy( $token );
  31. ?>


Wczesniej jednak musisz dac
  1. <?php
  2. ?>


Ten post edytował paweb 4.12.2005, 22:58:38


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
niko
post 12.12.2005, 07:03:47
Post #7





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


a tam trzeba wgrywać jakieś obrazki ? bo u mnie jest w kodzie np:

<img src="token.php?t=3859C53D" alt="">

ale obrazek sie nie wyświetla - jest X tak jakby go brakowało sad.gif

czego mi brakuje ?
Go to the top of the page
+Quote Post
sajborg
post 12.12.2005, 08:09:06
Post #8





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


Obraz tłą
  1. <?php
  2. $img["file"] = @imagecreatefromjpeg ("token_background.jpg");
  3.  
  4. ?>


Czcionki
  1. <?php
  2. $font["files"] = array ( "stilltim.ttf", "dipedm.ttf", "zoinkfat.ttf" );
  3. ?>

Jak chcesz jedna to daj
  1. <?php
  2. $font["file"] = "stilltim.ttf" ;
  3. ?>


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
niko
post 12.12.2005, 09:15:15
Post #9





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


mam tak :

plik token.php

  1. <?
  2. $img["width"] = 240;
  3. $img["height"] = 85;
  4. $img["banned"] = array("#0#si", "#O#si");
  5. $img["text"] = reg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  6. $_SESSION["tokenid"]=$img["text"];
  7. $img["file"] = @imagecreatefromjpeg ("images/token.jpg");
  8.  
  9. $font["angle"] = rand(-5,5);
  10. $font["files"] = array ( "czcionki/aurorac.ttf", "czcionki/bauhausb.ttf", "czcionki/boltb.ttf" );
  11. $font["rand"] = array_rand ( $font["files"] );
  12. $font["file"] = $font["files"][$font["rand"]];
  13.  
  14. $token = imagecreatetruecolor ( $img["width"], $img["height"] );
  15.  
  16. $color["white"] = imagecolorallocate ( $token, 0,0,);
  17.  
  18. imagecopy ( $token, $img["file"], 0, 0, 0, 0, $img["width"], $img["height"] );
  19.  
  20. $box = imagettfbbox ( 24, $font["angle"], $font["file"], $img["text"] );
  21.  
  22. $x = (int) ( ( $img["width"] - abs ( max ( $box[2], $box[4] ) ) ) / 2);
  23. $y = (int) ( ( $img["height"] + abs ( max ( $box[5], $box[7] ) ) ) / 2);
  24.  
  25. imagecolorallocate ( $token, 0, 0, 0 );
  26. imagettftext ( $token, 24, $font["angle"], $x, $y, $color["white"], $font["file"], $img["text"] );
  27.  
  28. header ( 'Content-type: image/gif' );
  29. imagegif ( $token );
  30. imagedestroy( $token );
  31. ?>


plik z formem :

  1. <?
  2. $banned = array("#0#si", "#O#si");
  3. $tekst = preg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  4. echo '<img src="token.php?t='.$tekst.'" alt="">';
  5. ?>


wyświetla mi w źródle : <img src="token.php?t=ECE6B8A6" alt="">

ale obrazek sie nie pokazuje - jest X
Go to the top of the page
+Quote Post
FiDO
post 12.12.2005, 12:25:33
Post #10





Grupa: Przyjaciele php.pl
Postów: 1 717
Pomógł: 0
Dołączył: 12.06.2002
Skąd: Wolsztyn..... Studia: Zielona Góra

Ostrzeżenie: (0%)
-----


Przeklej adres tego obrazka bezposrednio do przegladarki i zobacz jak wyswietli Ci blad.


--------------------
Brak czasu :/
Go to the top of the page
+Quote Post
niko
post 12.12.2005, 13:38:40
Post #11





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


wywala coś takiego:

Fatal error: Call to undefined function: reg_replace() in /home/XXXXX/public/domena-pl/token.php on line 19
Go to the top of the page
+Quote Post
mike
post 12.12.2005, 13:42:59
Post #12





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


Masz coś takiego:
  1. <?php
  2.  
  3. //...
  4. $img["text"] = reg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  5. //...
  6.  
  7. ?>

i się dziwisz że są błędy.

Mógłbyś pomyśleć i wywnioskować że nie ma takiej funkcji jak reg_replace.

Literówkę walnąłeś.
Go to the top of the page
+Quote Post
niko
post 12.12.2005, 15:47:56
Post #13





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


Spoko poprawiłem literówkę na preg_replace i wywala mi coś takiego :


Warning: preg_replace(): Empty regular expression in /home/XXX/public/domena-pl/token.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/XXX/public/domena-pl/token.php:18) in /home/XXX/public/domena-pl/token.php on line 41
GIF87ađU€üţü,đUţ„Ź&copy;ËíŁś´Ú‹ł Ľű†âH–ć‰&brvbar;ę&para;îđL×öŤçş'ďţ …˝ˇńˆL&‹&brvbar;ó M1ŁÔŞ9˝j&middot;ÜY&para; ‹Aß&plusmn;ů|-Ł×ě% Źcßňş]§ľë÷§<˙ŘáH¨7Xˆw˜Čˆ&para;ŘöI&copy;5Y‰u™É&copy;´Ů :ôJę3ZŠzsš łÚ &raquo;ňKk2[‹C—Ë[sŰËű Ś+<L[l ‹śĚşĚŚęüL- J]ÍyŤŤ&copy;˝MŮí Î8NŽh~N˜&reg;ČŢÎ÷o¸;o,o_‡źŻXĎěď&plusmn;€Ź,¨ě Âf BkčpĈÖ&RĚfń"&middot;WŚżqě(î#Čr"G˘+ir”îV˛Śçň%=™ivÚgó ÎśMvňtół’Ď F†%󨣤JÍmjŠ)TIR§vyjÖ&not;64xý 6&not;Ř&plusmn;dË.(;

czyli cosik jeszcze nie łapie sad.gif
Go to the top of the page
+Quote Post
sajborg
post 12.12.2005, 16:03:05
Post #14





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


token.php
  1. <?
  2. $img["width"] = 240;
  3. $img["height"] = 86;
  4. $img["file"] = @imagecreatefromjpeg ("images/token.jpg");
  5.  
  6. $font["angle"] = rand(-5,5);
  7. $font["files"] = array ( "czcionki/aurorac.ttf", "czcionki/bauhausb.ttf", "czcionki/boltb.ttf" );
  8. $font["rand"] = array_rand ( $font["files"] );
  9. $font["file"] = $font["files"][$font["rand"]];
  10.  
  11. $token = imagecreatetruecolor ( $img["width"], $img["height"] );
  12.  
  13. $color["white"] = imagecolorallocate ( $token, 0,0,);
  14.  
  15. imagecopy ( $token, $img["file"], 0, 0, 0, 0, $img["width"], $img["height"] );
  16.  
  17. $box = imagettfbbox ( 24, $font["angle"], $font["file"], $img["text"] );
  18.  
  19. $x = (int) ( ( $img["width"] - abs ( max ( $box[2], $box[4] ) ) ) / 2);
  20. $y = (int) ( ( $img["height"] + abs ( max ( $box[5], $box[7] ) ) ) / 2);
  21.  
  22. imagecolorallocate ( $token, 0, 0, 0 );
  23. imagettftext ( $token, 24, $font["angle"], $x, $y, $color["white"], $font["file"], $img["text"] );
  24.  
  25. header ( 'Content-type: image/gif' );
  26. imagegif ( $token );
  27. imagedestroy( $token );
  28. ?>


index.php
  1. <?
  2. $banned = array("#0#si", "#O#si");
  3. $tekst = preg_replace( $banned, "X", strtoupper ( substr ( md5 ( uniqid ( rand(), true ) ), 0, 8 ) ) );
  4. echo '<img src="token.php?t='.$tekst.'" alt="">';
  5. ?>


U mnie działa


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
niko
post 12.12.2005, 16:33:03
Post #15





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


nie wiem - ja mam użyte to w pliku add.php

w pliku tym jest juz sesja uruchomiona

daje to co macie w index.php a w token.php :

- raz dowala mi sie do tego ze nagłówek mam juz odpalony

header ( 'Content-type: image/gif' ); - czyli to

no i wywala mi coś takiego :

ze niby jakies tam puste wyrazenie albo co - niezdążyłem tego złapac


dałem token.php?t=C63A4D21

i wyświetliło mi zdjęcie ale czyste - dałem białego jpga jako tło i nic zero napisów

echo '<img src="token.php?t='.$tekst.'" alt="">';

a gdzie uzyte to t jest ? w token.php chyba zmienna t powinna gdzieś być ?

Ten post edytował niko 12.12.2005, 16:44:09
Go to the top of the page
+Quote Post
sajborg
post 12.12.2005, 18:35:59
Post #16





Grupa: Zarejestrowani
Postów: 271
Pomógł: 0
Dołączył: 20.08.2003
Skąd: Szczecinek

Ostrzeżenie: (0%)
-----


zapomnialem co za problem dodac
  1. <?php
  2. $img["text"]=$_GET['t'];
  3. ?>


--------------------
Warsztat: gPHPEdit | Linux 2.6.17-11-generic (Ubuntu)
Sprzęt: Noteook BenQ A33E
___________________________________________________
Go to the top of the page
+Quote Post
niko
post 13.12.2005, 10:16:48
Post #17





Grupa: Zarejestrowani
Postów: 109
Pomógł: 0
Dołączył: 2.06.2005

Ostrzeżenie: (30%)
XX---


no teraz działa smile.gif

a pytanie mam smile.gif jak zrobić by jednocześnie budował obrazek używając różnych czcionek ? teraz losowo sobie je wybiera i jak wybierze to wszystkie znaczki są tą samą czcionką. a jak zrobić by np co 2 wymienial czcionkę ?

to samo jesli chodzi o kolor napisów i ich wielkość /liter/

teraz jak to wykorzystać przy formularzu ? jak sprawdzić czy dobrze to przepisał po wysłaniu ?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.07.2025 - 02:04