2

Im trying to use an if function within the else function of a preg_match command.

$month1data is fetched from CURL and has been checked to work.

The following code is:

global $attempt;
$attempt = mysql_escape_string($_GET['attempt']);

if (preg_match('/<td colspan=8(.*)<\/table/s', $month1data, $matches)) {
    //Content found do stuff here

    unset ($ch);
    unset ($cache);
    unset ($firstmonthdata);
    unset ($matches);
} else { // start else preg match
    if ($attempt = '3') { //start if attempt = 3

        echo 'failed 3 times - showing error';
        echo '<script type="text/javascript">
        <!--
        window.location = "http://www.website.com/error.php?error=2"
        //-->
        </script>';

    } // end if attempt = 3
    else { //start if attempt dont = 3

        echo 'keep trying for 3 times';
        $attempt = $attempt +1;
        echo '<script type="text/javascript">
        <!--
        window.location = "http://www.website.com/page.php?email=' . $email . '&password=' . $password . '&attempt=' . $attempt . '"
        //-->
        </script>';
    }// end if attempt dont 3 else
} // end else preg match

However whenever the page is loaded it directs straight to the error page:

"http://www.website.com/error.php?error=2"

I have looked at several others posts but can't see what's wrong, is it possible to implement these methods in this way or is there just something missing?

2 Answers 2

5

It should be:

if ($attempt === '3') {
             ^^^

You are assigning 3 to $attempt.

Or even better as there seems no database involved:

$attempt = (int) $_GET['attempt'];

...

if ($attempt === 3) {

Edit: Apart from that you would be better off using a session for this kind of attempt checking as the visitor can easily manipulate the query string.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank-you, I can't believe I missed this, shows why you shouldn't overlook the simple things!
@Jack, see stackoverflow.com/a/13783985/1592648 for a tip on how to avoid mistakes like this in the future. =o)
2

The following

if ($attempt = '3')

Should be

if ($attempt == '3')

2 Comments

Thank-you, I can't believe I missed this, shows why you shouldn't overlook the simple things!
Don't forget to mark one of the answers as the correct answer, :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.