Jump to content

[SOLVED] the captcha session shows not the same number as the post captcha


Recommended Posts

here is my code:

 

session_start();
$_SESSION["captchika"] = rand(100,999);
$_SESSION["datika"] = getenv("REMOTE_ADDR");

if(isset($_POST['ptitle'])){
if(addslashes($_POST["captcha"]) != session_regenerate_id($_SESSION["captchika"], ENT_QUOTES)){die("You left some fields empty!");}
elseif((!empty($_POST['ptitle'])) && (!empty($_POST['pcontent'])) && (!empty($_POST['pcategory']))){
if(mysql_query("INSERT INTO posts (ptitle,pcontent,pcategory,pdate) VALUES ('".addslashes($_POST['ptitle'])."','".addslashes($_POST['pcontent'])."','".addslashes($_POST['pcategory'])."','".$_SESSION["datika"]."')")){echo" Posted! ";}
//header("Location: ".$site_path."");
}else{echo"You have entered the wrong number!";}
}

echo '<br /><form method="post">
Title:<br />
<input type="text" size="50" name="ptitle" />
<button type="submit">Post</button><br /><br />
Category:<br />
<select name="pcategory">
<option value="Chat">Chat</option>
<option value="Help">Help</option>
<option value="Tutorial">Tutorial</option>
<option value="BuySell">BuySell</option>
<option value="Request">Request</option>
</select><br /><br />
Content:<br />
<textarea name="pcontent" cols="50" rows="7"></textarea><br />
Enter This Number: '.$_SESSION["captchika"].' <input type="text" name="captcha" /><br />
</form><br />';

Because you are generaing a new random number and assigning it to the session variable every time the page gets requested. You should only generate the random number and assign it to the session variable when the form is displayed, not every time the page is requested (when a form is submitted to a page, that causes a request for that page.)

i know... i figured that out, that when i use a $_POST variable then the site refreshes or something like that and the number changes... :S i just don't know how to fix this. :S if i have only one if statement then it does work but then i can't check if the other fields are empty. :S

 

how should i recode it s that i will work?

 

thank you, tastro

Change

session_start();
$_SESSION["captchika"] = rand(100,999);
$_SESSION["datika"] = getenv("REMOTE_ADDR");

if(isset($_POST['ptitle'])){
if(addslashes($_POST["captcha"]) != session_regenerate_id($_SESSION["captchika"], ENT_QUOTES)){die("You left some fields empty!");}
elseif((!empty($_POST['ptitle'])) && (!empty($_POST['pcontent'])) && (!empty($_POST['pcategory']))){
if(mysql_query("INSERT INTO posts (ptitle,pcontent,pcategory,pdate) VALUES ('".addslashes($_POST['ptitle'])."','".addslashes($_POST['pcontent'])."','".addslashes($_POST['pcategory'])."','".$_SESSION["datika"]."')")){echo" Posted! ";}
//header("Location: ".$site_path."");
}else{echo"You have entered the wrong number!";}
}

echo '<br /><form method="post">
Title:<br />
<input type="text" size="50" name="ptitle" />
<button type="submit">Post</button><br /><br />
Category:<br />
<select name="pcategory">
<option value="Chat">Chat</option>
<option value="Help">Help</option>
<option value="Tutorial">Tutorial</option>
<option value="BuySell">BuySell</option>
<option value="Request">Request</option>
</select><br /><br />
Content:<br />
<textarea name="pcontent" cols="50" rows="7"></textarea><br />
Enter This Number: '.$_SESSION["captchika"].' <input type="text" name="captcha" /><br />
</form><br />';

 

to

 

session_start();

if(isset($_POST['ptitle'])){
if(addslashes($_POST["captcha"]) != session_regenerate_id($_SESSION["captchika"], ENT_QUOTES)){die("You left some fields empty!");}
elseif((!empty($_POST['ptitle'])) && (!empty($_POST['pcontent'])) && (!empty($_POST['pcategory']))){
if(mysql_query("INSERT INTO posts (ptitle,pcontent,pcategory,pdate) VALUES ('".addslashes($_POST['ptitle'])."','".addslashes($_POST['pcontent'])."','".addslashes($_POST['pcategory'])."','".$_SESSION["datika"]."')")){echo" Posted! ";}
//header("Location: ".$site_path."");
}else{echo"You have entered the wrong number!";}
}

$_SESSION["captchika"] = rand(100,999);
$_SESSION["datika"] = getenv("REMOTE_ADDR");

echo '<br /><form method="post">
Title:<br />
<input type="text" size="50" name="ptitle" />
<button type="submit">Post</button><br /><br />
Category:<br />
<select name="pcategory">
<option value="Chat">Chat</option>
<option value="Help">Help</option>
<option value="Tutorial">Tutorial</option>
<option value="BuySell">BuySell</option>
<option value="Request">Request</option>
</select><br /><br />
Content:<br />
<textarea name="pcontent" cols="50" rows="7"></textarea><br />
Enter This Number: '.$_SESSION["captchika"].' <input type="text" name="captcha" /><br />
</form><br />';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.