1

単語キャプチャや数学キャプチャなどの簡単なキャプチャを登録フォームに追加する必要があります。ただし、コードは機能していません。以下はコードです:(コードを修正するのを手伝ってください)

<?php
session_start();
//random string generator function
function random($length){
//create a or array string called chars
        $chars ="abcdefghijklmnopqrstuvwxyz23456789";
         $str = "";
        //is the variable which is equal to the length of the string called chars
        $size = strlen($chars);
        for($i=0; $i<$length; $i++){
             $str .= $chars[rand(0, $size-1)];

    }
    return $str;
}
//the gd image adaptor in xampp
$cap = random(7);
$_SESSION['real'] = $cap;
$image = imagecreate(100, 20);
$background = imagecolorallocate($image, 0, 0, 0);
$foreground = imagecolorallocate($image, 255, 255, 255);
//this is going to write our string in the image
imagestring($image, 5,5,1,$cap,$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
?>

登録ページコード

   <?php
//This function will display the registration form
function register_form(){

$date = date('D, M, Y');
echo "<form action='?act=register' method='post'>"
    ."Username: <input type='text' name='username' size='30'><br>"
    ."Password: <input type='password' name='password' size='30'><br>"
    ."Confirm your password: <input type='password' name='password_conf' size='30'><br>"
    ."Email: <input type='text' name='email' size='30'><br>"
    ."<img src= 'captcha.php'>  <input type='text' name='captcha'<br>"
    ."<input type='hidden' name='date' value='$date'>"
    ."<input type='submit' value='Register'>"
    ."</form>";

}

//This function will register users data
function register(){

//Connecting to database
$connect = mysql_connect("localhost", "root", "nokiae71");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("forumStructure", $connect);
if(!$select_db){
die(mysql_error());
}

//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];
$guess = $_POST['captcha'];

//Here we will check do we have all inputs filled

if(empty($username)){
die("Please enter your username!<br>");
}

if(empty($password)){
die("Please enter your password!<br>");
}

if(empty($pass_conf)){
die("Please confirm your password!<br>");
}

if(empty($email)){
die("Please enter your email!");
}


$real = (isset($_SESSION['real'])) ? $_SESSION['real'] : "";


if (empty($guess))
{ 
    die("Please enter the correct CAPTCHA.<br>");
} 
/*if ($real != $guess){
    die("things are bad");
}*/
if (!empty($guess) && $guess !== $real)
{
    die("Please enter the correct CAPTCHA.<br>");
}

//Let's check if this username is already in use

$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

//Now if email is already in use

$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

//Now display errors

if($do_user_check > 0){
die("Username is already in use!<br>");
}

if($do_email_check > 0){
die("Email is already in use!");
}

//Now let's check does passwords match

if($password != $pass_conf){
die("Passwords don't match!");
}


//If everything is okay let's register this user

$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}

echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";

}
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?> 

ありがとう、私はPHPの初心者で、これを行う方法を学びたいと思っています。学習のみを目的として、stackoverflow で登録コードを取得しました...ご協力やご提案をお待ちしております。

4

2 に答える 2

1

session_start(); がありません。上部と > の <img src= 'captcha.php'> <input type='text' name='captcha'><br>"

于 2012-12-27T18:55:51.927 に答える