tastro Posted October 8, 2009 Share Posted October 8, 2009 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 />'; Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 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.) Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933193 Share on other sites More sharing options...
tastro Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933214 Share on other sites More sharing options...
tastro Posted October 9, 2009 Author Share Posted October 9, 2009 pls help anyone ? :S Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933582 Share on other sites More sharing options...
xsist10 Posted October 9, 2009 Share Posted October 9, 2009 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 />'; Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933585 Share on other sites More sharing options...
tastro Posted October 9, 2009 Author Share Posted October 9, 2009 loooooool, thank you m8. it works, omg i just had to move the $_SESSION 's down xD wtf... but i think that i have tryed this already but didn't work. :S and now it works. thank you, tastro Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933614 Share on other sites More sharing options...
xsist10 Posted October 9, 2009 Share Posted October 9, 2009 No problem Quote Link to comment https://forums.phpfreaks.com/topic/176995-solved-the-captcha-session-shows-not-the-same-number-as-the-post-captcha/#findComment-933619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.