NLT Posted March 16, 2012 Share Posted March 16, 2012 I'm making a password reset script, but all in one page. I've currently got an if and an else. This is something like it: if($_POST['submit']){ // Do things } else { // Display form } But I want another if (maybe?) for a $GET statement. So if somebody accesses my page with ?code=somethinghere then it will display something. How would I go about it? Quote Link to comment Share on other sites More sharing options...
Ptsface12 Posted March 16, 2012 Share Posted March 16, 2012 Hello, If I understand you correctly, you want to check if the POST button is submitted, if so, retrieve it from the URL, correct? If so, here's the code: if($_POST['submit']){ // Checks if the form is submitted. if($_GET['code']) { // Gets the ?code value from the url echo $_GET['code']; // Echos back the ?code data } else { // Form data here. } Quote Link to comment Share on other sites More sharing options...
NLT Posted March 16, 2012 Author Share Posted March 16, 2012 No. I want to make a password reset script but all in one page. if($_POST['submit']){ // In here it inserts a code into a table to send out a password } else { // Display form } But, for the code to be activated, and a password to be sent out I would need to use $_GET. As such: url?code=codegoeshere I want it to display something completely different, and add stuff inside it (with some if's). I hope you understand, and thank you. EDIT: I've tried to use an if statement for it.. but I'm not sure how I'd get the code because it didn't work for me.. I used: if($_GET['code'] { } Quote Link to comment Share on other sites More sharing options...
Decepti0n Posted March 16, 2012 Share Posted March 16, 2012 Do you mean something like this? <?php /* To keep URL's shorter, add extended text to an array and use a shorter form in the url. Create the array before the $_GET is checked, so you can also check against the array. For an example, I will use error and warning as code=?? examples */ $code_array = array ( 'error' => 'There has been an error in the script', 'warning' => 'Warning, your IP has been logged by the system for suspicious activities' ); // Now check if $_GET['code'] is defined and it exists within the array if(array_key_exists('code', $_GET) && in_array($_GET['code'], $code_array)) { // If you get here, the item `code` exists within the $_GET array and is also within the $code_array echo $code_array[ $_GET['code'] ]; } Quote Link to comment Share on other sites More sharing options...
NLT Posted March 16, 2012 Author Share Posted March 16, 2012 I used that, but now, when I type password?code=lol it takes me straight to the form. Would you like to see the script? Quote Link to comment Share on other sites More sharing options...
Decepti0n Posted March 16, 2012 Share Posted March 16, 2012 Fire away. Could you also be more clear on what exactly you plan to pass to the $_GET -- then I can draft that together too Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2012 Share Posted March 16, 2012 Why do you want to make all these into one page? I always find it best to have my logic broken down into small files - then include the ones as needed. Anyway, there are several ways to put this logic together. Here is one example if(isset($_GET['code'])) { //Perform activation code here } else { if(isset($_POST['submit'])) { //Perform form validation/processing processes //If everything passes redirect to a confirmation page //If validation fails then code will drop-them back down to the form } //Add code here to show form } Quote Link to comment Share on other sites More sharing options...
litebearer Posted March 16, 2012 Share Posted March 16, 2012 I THINK he/she means... if get isset do this else if submit isset do that else do something else ASIDE isset submit may not always work. Better to check if a particular REQUIRED form field isset Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2012 Share Posted March 16, 2012 I THINK he/she means... if get isset do this else if submit isset do that else do something else ASIDE isset submit may not always work. Better to check if a particular REQUIRED form field isset Yes, I know what was meant and I could have written that very easily. if(isset($_GET['code'])) { //Activation code } elseif(isset($_POST['submit']) { //Process post data } else { //Show form } But, if you are using the same script for the form and processing it is typical to have the processing logic drop-back to the form when there is a validation error. The logic I first provided will work exactly the same as the above but with the added benefit of being able to revert back to the form when there is an error in the POST data. Regarding the isset() on the 'SUBMIT' value, I wouldn't even check a required field. Instead, I would check the REQUEST_METHOD. But, that is another subject that I felt would confuse the issue at hand. Quote Link to comment Share on other sites More sharing options...
NLT Posted March 17, 2012 Author Share Posted March 17, 2012 Why do you want to make all these into one page? I always find it best to have my logic broken down into small files - then include the ones as needed. Anyway, there are several ways to put this logic together. Here is one example if(isset($_GET['code'])) { //Perform activation code here } else { if(isset($_POST['submit'])) { //Perform form validation/processing processes //If everything passes redirect to a confirmation page //If validation fails then code will drop-them back down to the form } //Add code here to show form } Because it's my first time in using the $_GET method. I'll probably try clean it up another time, but I want to experience it, if you know what I mean. <?PHP if(isset($_GET['code'])) { echo $_GET['code']; } // Check if form has been submit already elseif(isset($_POST['submit'])) { // Look for their user $lookuser = mysql_query("SELECT * FROM `users` WHERE username='". mysql_escape_string($_POST['username']) ."'"); // If we find a row if(mysql_num_rows($lookuser) > 0) { $uinfo = mysql_fetch_assoc($lookuser); $chktable = mysql_query("SELECT * FROM `passwordrecovery` WHERE username='". mysql_escape_string($_POST['username']) ."'"); if(mysql_num_rows($chktable)==0) { $hash=md5(uniqid(rand())); $query1 = mysql_query("INSERT INTO `passwordrecovery`(username, hash) VALUES ('". $uinfo['username'] ."', '". $hash ."')"); echo "Thank you, ". $uinfo['username'] .". Please check your email (It may appear in the Junk folder). "; $to = "$uinfo['mail']"; $subject = "Your new password"; $header = "test"; $message="Your confirmation link\r\n"; $message.="Click on this link to have a password sent to you\r\n"; $sentmail = mail($to, $subject, $header, $message); } else { $deletekey = mysql_query("DELETE FROM `passrecovery` WHERE username='". $uinfo['username'] ."'"); echo "Please reload the page"; } } // If no row was found else { echo "An error has occured. <br> If you are sure you entered your username correctly, please contact an administrator."; } } else { ?> <form method="post" action="URL"> <input type="text" name="username" /> <input type="submit" name="submit" value="Submit" /> </form> <?PHP } ?> What I used is that, and when I type: URL/password?code=lol then it just takes me to the form. If there's another way I could do this, fire away. Quote Link to comment Share on other sites More sharing options...
litebearer Posted March 17, 2012 Share Posted March 17, 2012 what do you want to happen if GET is set? Quote Link to comment Share on other sites More sharing options...
NLT Posted March 17, 2012 Author Share Posted March 17, 2012 <?PHP $code = $_GET['code']; $query = mysql_query("SELECT * FROM `passwordrecovery` WHERE username='". $uinfo['username'] ."'"); if(mysql_num_rows($query) > 0) { $randompass = "FOJSDAOGUE3o9r5ujogu"; $doquery = mysql_query("UPDATE `users` SET password='". $randompass ."' WHERE username='". $uinfo['username'] ."'"); echo "Your password has been reset and sent to your email."; // Email code here } else { echo "Your confirmation code was invalid."; } ?> That is an example of what I want to happen. Quote Link to comment Share on other sites More sharing options...
NLT Posted March 17, 2012 Author Share Posted March 17, 2012 Any help? Quote Link to comment Share on other sites More sharing options...
NLT Posted March 18, 2012 Author Share Posted March 18, 2012 Can anybody help me regarding this? Quote Link to comment Share on other sites More sharing options...
litebearer Posted March 18, 2012 Share Posted March 18, 2012 You realize that in your latest example you are NOT using $code? Quote Link to comment Share on other sites More sharing options...
NLT Posted March 18, 2012 Author Share Posted March 18, 2012 You realize that in your latest example you are NOT using $code? I'm not sure what you quite mean.. Quote Link to comment Share on other sites More sharing options...
trq Posted March 18, 2012 Share Posted March 18, 2012 You realize that in your latest example you are NOT using $code? I'm not sure what you quite mean.. At the top of your script you define $code: $code = $_GET['code']; Then never use it for anything. Quote Link to comment Share on other sites More sharing options...
NLT Posted March 18, 2012 Author Share Posted March 18, 2012 You realize that in your latest example you are NOT using $code? I'm not sure what you quite mean.. At the top of your script you define $code: $code = $_GET['code']; Then never use it for anything. Yes, I know, that was just an example to what I want the script to do. I did try make $code echo, but it just returns me to the form. Quote Link to comment 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.