Sicherheitscode - nur auf Zahlen begrenzen

  • Hallo zusammen!
    Sooo, hoffentlich kann mir der eine oder andere hier weiterhelfen. Hab absolut keine Ahnung von php usw ... dies schon mal vorab.
    Wäre es möglich den unten stehenden code so zu verändern dass er mir nur Zahlen von 0-9 ausgibt?

    Schonmal vielen Dank im Voraus!!


    <?php
    function xtc_rand($min = null, $max = null) {
    static $seeded;

    if (!isset($seeded)) {
    mt_srand((double)microtime()*1000000);
    $seeded = true;
    }

    if (isset($min) && isset($max)) {
    if ($min >= $max) {
    return $min;
    } else {
    return mt_rand($min, $max);
    }
    } else {
    return mt_rand();
    }
    }
    ?>

  • Danke, ersmal für die flotte Antwort!

    Puhh, wie gesagt hab ich nich wirklich die Ahnung. Die ganze Sache ist aus einer Sicherheitsabfrage für nen Newsletter. Ein zweites Script gibt anschlissend aus verschiedenen ttf (Fonts) Zahlen und Grossbuchstaben aus, welche aber aufgrund der verschiedenen Fonts für viele User verwirrend rüberkommen z.b 0Oo1liI usw. , sprich der User hat des öfteren Fehleingaben und muss ständig einen neuen Code eingeben ... wenn er nicht genervt abbricht. Hier mal die scripte, die meiner Meinung nach eine Rolle spielen. Die Zahlen, welche dieses Script auswirft, dienen wohl zum "Nachschlagen" in den ttf´s , sprich jede Zahl steht für ein entsprechendes Zeichen.


    ***xtc_rand.inc.php***
    function xtc_rand($min = null, $max = null) {
    static $seeded;

    if (!isset($seeded)) {
    mt_srand((double)microtime()*1000000);
    $seeded = true;
    }

    if (isset($min) && isset($max)) {
    if ($min >= $max) {
    return $min;
    } else {
    return mt_rand($min, $max);
    }
    } else {
    return mt_rand();
    }
    }
    ?>

    In diesem Script wird wohl über "strtoupper" geregelt dass nur Grossbuchstaben ausgegeben werden, welche anschliessend als Grafik dargestellt werden.

    ****xtc_render_vvcode.inc.php***
    require_once(DIR_FS_INC . 'xtc_rand.inc.php');

    function vvcode_render_code($code) {
    if (!empty($code)) {

    // load fonts
    $ttf=array();
    if ($dir= opendir(DIR_WS_INCLUDES.'fonts/')){
    while (($file = readdir($dir)) !==false) {
    if (is_file(DIR_WS_INCLUDES.'fonts/'.$file) and (strstr(strtoupper($file),'.TTF'))){
    $ttf[]=DIR_FS_CATALOG.'/includes/fonts/'.$file;
    }
    }
    closedir($dir);
    }
    $width = 240;
    $height =50;

    $imgh = imagecreate($width, $height);

    $fonts = imagecolorallocate($imgh, 112, 112, 112);
    $lines = imagecolorallocate($imgh, 220, 148, 002);
    $background = imagecolorallocate($imgh, 196, 196, 196);
    imagefill($imgh, 0, 0, $background);

    $x = xtc_rand(0, 20);
    $y = xtc_rand(20, 40);
    for ($i = $x, $z = $y; $i < $width && $z < $width;) {
    imageLine($imgh, $i, 0, $z, $height, $lines);
    $i += $x;
    $z += $y;
    }

    $x = xtc_rand(0, 20);
    $y = xtc_rand(20, 40);
    for ($i = $x, $z = $y; $i < $width && $z < $width;) {
    imageLine($imgh, $z, 0, $i, $height, $lines);
    $i += $x;
    $z += $y;
    }

    $x = xtc_rand(0, 10);
    $y = xtc_rand(10, 20);
    for ($i = $x, $z = $y; $i < $height && $z < $height;) {
    imageLine($imgh, 0, $i, $width, $z, $lines);
    $i += $x;
    $z += $y;
    }

    $x = xtc_rand(0, 10);
    $y = xtc_rand(10, 20);
    for ($i = $x, $z = $y; $i < $height && $z < $height;) {
    imageLine($imgh, 0, $z, $width, $i, $lines);
    $i += $x;
    $z += $y;
    }

    for ($i = 0; $i < strlen($code); $i++) {
    $font = $ttf[(int)xtc_rand(0, count($ttf)-1)];
    $size = xtc_rand(30, 36);
    $rand = xtc_rand(1,20);
    $direction = xtc_rand(0,1);

    if ($direction == 0) {
    $angle = 0-$rand;
    } else {
    $angle = $rand;
    }
    if (function_exists('imagettftext')) {
    imagettftext($imgh, $size, $angle, 15+(36*$i) , 38, $fonts, $font, substr($code, $i, 1));
    } else {
    $tc = ImageColorAllocate ($imgh, 0, 0, 0); //Schriftfarbe - schwarz
    ImageString($imgh, $size, 26+(36*$i),20, substr($code, $i, 1), $tc);
    }
    }

    header('Content-Type: image/jpeg');
    imagejpeg($imgh);
    imagedestroy($imgh);
    }
    }
    ?>

    Wie gesagt wären mir ausschl. Zahlen lieber ... um diese Fehleingaben auszuschliessen.

    Soooo, sollte noch irgendwas benötigt werden ... kann ja sein ... stell ichs gern zur Verfügung.


    Vielen Dank schonmal!!

    Piels

  • strtoupper wandelt eine string in reine großbuchstaben um.

    das sit es nicht und die funktion die du gepostet hast, erzeugt halt nur eien zufallszahl..

    vcode_render_code($code) der coe wird heir schon übergeben....

    also musst du danach suchen woi die funktion aufgerufen wird...

  • Danke GreenRover, für die schnellen Antworten!

    Hab das entsprechende Script nach massig suchen und probieren gefunden. Fündig geworden bin ich in

    +++xtc_random_charcode.inc+++

    // build to generate a random charcode
    function xtc_random_charcode($length) {
    $arraysize = 34;
    $chars = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9');

    $code = '';
    for ($i = 1; $i <= $length; $i++) {
    $j = floor(xtc_rand(0,$arraysize));
    $code .= $chars[$j];
    }
    return $code;
    }
    ?>

    Einfach die entsprechenden Buchstaben raus genommen arraysize geändert ... und ich hab meine Zahlen. Und das gesamte Script läuft auch noch ;)

    Wie gesagt ... danke für die flotten Antworten!

    Piels