Autor Zpráva
Chatík
Profil
Ahoj,

potřeboval bych radu proč následující script mi hlásí tuto chybu:
Fatal error: Call to undefined function getRandomString() in na vyznačeném řádku.


              <?php

if(!isset($_GET['email'])){
                      echo'<form action="forgotpassword.php">
                          Enter Your Email Id:
                             <input type="text" name="email" />
                            <input type="submit" value="Reset My Password" />
                             </form>'; 
                       } else{   
$email=$_GET['email'];
include("db.php");

$q="select mail from uzivatele where mail='".$email."'";
$r=mysql_query($q);
$n=mysql_num_rows($r);
if($n==0){echo "Email id is not registered"; die();}
$token=getRandomString(10);
$q="insert into tokens (token,email) values ('".$token."','".$email."')";
mysql_query($q);
function getRandomString($length) 
       {
    $validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
    $validCharNumber = strlen($validCharacters);
    $result = "";
 
    for ($i = 0; $i < $length; $i++) {
        $index = mt_rand(0, $validCharNumber - 1);
        $result .= $validCharacters[$index];
    }
    return $result;}
 function mailresetlink($to,$token){
$subject = "Forgot Password on example.com";
$uri = 'http://'. $_SERVER['HTTP_HOST'] ;
$message = '
<html>
<head>
<title>Forgot Password For example.com</title>
</head>
<body>
<p>Click on the given link to reset your password <a href="'.$uri.'/reset.php?token='.$token.'">Reset Password</a></p>

</body>
</html>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Admin<webmaster@example.com>' . "\r\n";
$headers .= 'Cc: Admin@example.com' . "\r\n";

if(mail($to,$subject,$message,$headers)){
    echo "We have sent the password reset link to your  email id <b>".$to."</b>"; 
}}

if(isset($_GET['email']))mailresetlink($email,$token);  }
?>



Samozřejmě že chyba byla mezi klávesnicí a židlí.

Volal jsem funkci dřív než byla vůbec vytvořena.

Zde už je to správně:
                <?php

if(!isset($_GET['email'])){
                      echo'<form action="forgotpassword.php">
                          Enter Your Email Id:
                             <input type="text" name="email" />
                            <input type="submit" value="Reset My Password" />
                             </form>'; 
                       } else{   
$email=$_GET['email'];
include("db.php");

$q="select mail from uzivatele where mail='".$email."'";
$r=mysql_query($q);
$n=mysql_num_rows($r);
if($n==0){echo "Email id is not registered"; die();}
function getRandomString($length) 
       {
    $validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789";
    $validCharNumber = strlen($validCharacters);
    $result = "";
 
    for ($i = 0; $i < $length; $i++) {
        $index = mt_rand(0, $validCharNumber - 1);
        $result .= $validCharacters[$index];
    }
    return $result;}
$token=getRandomString(10);
$q="insert into tokens (token,email) values ('".$token."','".$email."')";
mysql_query($q);
 function mailresetlink($to,$token){
$subject = "Forgot Password on example.com";
$uri = 'http://'. $_SERVER['HTTP_HOST'] ;
$message = '
<html>
<head>
<title>Forgot Password For example.com</title>
</head>
<body>
<p>Click on the given link to reset your password <a href="'.$uri.'/reset.php?token='.$token.'">Reset Password</a></p>

</body>
</html>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Admin<webmaster@example.com>' . "\r\n";
$headers .= 'Cc: Admin@example.com' . "\r\n";

if(mail($to,$subject,$message,$headers)){
    echo "We have sent the password reset link to your  email id <b>".$to."</b>"; 
}}

if(isset($_GET['email']))mailresetlink($email,$token);  }
?>
Bertram
Profil
Chatík:
Volal jsem funkci dřív než byla vůbec vytvořena.

Neměl jsi spíše někde překlep? To co popisuješ jako chybu, je v PHP přece možné.

<?php
// definice funkce až po jejím volání
$soucet = secti(5, 7);
echo $soucet;

function secti($a, $b){
  return $a+$b;
}
Jan Tvrdík
Profil
Bertram:
To sice ano, ale pouze v případě, že ta funkce je deklarována bezpodmínečně. Následující kód např. nebude fungovat

if (TRUE) {
    $soucet = secti(5, 7);
    echo $soucet;

    function secti($a, $b){
      return $a+$b;
    }
}
Bertram
Profil
Jan Tvrdík:
Dík za ujasnění, to jsem nevěděl. Ale ani jsem nad tím nikdy nepřemýšlel, protože mě nenapadá důvod, proč bych definoval funkci v nějaké podmínce.

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0