Jump to content

GET and IF


NLT

Recommended Posts

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?

Link to comment
Share on other sites

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.
} 

Link to comment
Share on other sites

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'] { }

Link to comment
Share on other sites

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'] ];
}

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.