Jump to content

PHP working differently in different browsers


trenttdogg
Go to solution Solved by cyberRobot,

Recommended Posts

Hello,

My issue is my php code working differently in different browsers. Here is the scenario:

When a user fills in a field on the form, it searches my db and it there is an EXACT match, it sends them to "page2.php", if not it should send them to a page called "tryagain.php". This works perfectly in Firefox, but in IE and Chrome it sends them to "page2.php" but the page is blank. I believe the issue is in the 'or die' piece of code below.

 

Any help would be greatly appreciated.

 

$data = 'SELECT * FROM `users` WHERE `username` = "'.$search.'"';
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $data2 = mysql_fetch_array($query)
or die ('<META HTTP-EQUIV="Refresh" CONTENT="User Not Found;URL=tryagain.php">');

Link to comment
Share on other sites

So much wrong here. The entire code is junk. Toss it and learn PDO. https://phpdelusions.net/pdo

If I wanted an answer from a smart A$$, I could have asked my 13 year old son. Also, if I had the time/desire to learn a all the programming languages, I'd probably be a developer already and not be in a forum asking "dumb" questions. Right?

So thanks anyway.

Link to comment
Share on other sites

  • Solution

Are you trying to get the page to display "User Not Found" before the redirect happens? If so, you can see the example at the bottom of the following page:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta

 

 

I think the issue is with the "User Not Found" text in the content attribute. Unless I missed something in the documentation, linked above, the value before the semi-colon needs to be a number.

 

Side note: PHP has a function for redirecting the page. More information can be found here:

http://php.net/manual/en/function.header.php

Link to comment
Share on other sites

1. The mysql extension and its mysql_* functions are old and unsupported. You need to start moving to either PDO (recommended) or mysqli.

2. I sure hope $search was escaped before you put it into that query.

3. die()ing with the MySQL error message is dangerous, not to mention a bad user experience.

 

A Refresh value needs to be structured as "seconds;url=path", with the first part being a number of seconds to wait to redirect.

 

4. A meta refresh (as it's called) is a poor way to move users between pages. Use a header() redirect if you haven't created any output or display a message with a link on the page if you have.

Link to comment
Share on other sites

You should have asked your 13 year old. He would have told you that your code is obsolete and has been completely removed from PHP and that your "code" is full of security vulnerabilities. PDO is not a new language. If you took a second to click on the link I provided you would have seen that. You are never going to be a programmer if you get butt hurt when someone tells you the truth.

Link to comment
Share on other sites

Are you trying to get the page to display "User Not Found" before the redirect happens? If so, you can see the example at the bottom of the following page:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta

 

 

I think the issue is with the "User Not Found" text in the content attribute. Unless I missed something in the documentation, linked above, the value before the semi-colon needs to be a number.

 

Side note: PHP has a function for redirecting the page. More information can be found here:

http://php.net/manual/en/function.header.php

Yes. It was the "user not found" text in the content field that was creating the problem. Thank you.

Link to comment
Share on other sites

You should have asked your 13 year old. He would have told you that your code is obsolete and has been completely removed from PHP and that your "code" is full of security vulnerabilities. PDO is not a new language. If you took a second to click on the link I provided you would have seen that. You are never going to be a programmer if you get butt hurt when someone tells you the truth.

I agree. I know my code sucks and is outdated and am a complete hack. But I am not trying to become a programmer or developer. Just wanted to know why it worked differntly in different browsers, which was because I had text where it should have be a numeral. PDO is obviously the better option, just dont know it.

Thanks.

Link to comment
Share on other sites

I wasn't trying to be an ***. Programmer's (me included) can be terse in answering. The PDO tutorial link I gave you is very good. You should be able to grasp it fairly easy. We all had to start somewhere. I was basically trying to keep you from wasting your time and provide you the correct direction. 

Link to comment
Share on other sites

What you'll (hopefully) realize soon is that a lot of the “smartass answers” are far more valuable than the literal answers.

 

When somebody asks you about the best way to drive a nail into the wall with an old shoe, you can tell them to use the sole. Or you can give them a hammer. Do you think the second option would make you an asshole who doesn't answer questions and wants everybody to become a professional craftsman? Or isn't it simply the right kind of help?

 

The proposed alternative to your code is this:

$userStmt = $databaseConnection->prepare('SELECT * FROM users WHERE username = :username');
$userStmt->execute(['username' => $search]);
$user = $userStmt->fetch();

if (!$user)
{
    echo 'User not found.<br><a href="tryagain.php">Go back</a>';    // not perfect, but definitely better than a page refresh
    exit;
}

This simultaneously

  • solves your problem
  • prevents your server from being compromised
  • makes your application survive the next PHP update
  • replaces the hacks with decent code

And it's not exactly rocket science either. Sure, you're new to PHP, but you don't seem to be stupid. So why not spend a few minutes on learning something new instead of wasting days on ancient stuff that you'll sooner or later have to rewrite anyway?

Link to comment
Share on other sites

Personally, I would put the form and the processing code into the same script. This is by far the easiest option and avoids “helper scripts” like this tryagain.php altogether.

 

If that isn't an option, I'd store the message in the session or a cookie. When you use URL parameters, the message may appear unexpectedly (reloads, bookmarks, URL sharing, ...).

Link to comment
Share on other sites

Personally, I would put the form and the processing code into the same script. This is by far the easiest option and avoids “helper scripts” like this tryagain.php altogether.

 

Agreed

 

 

If that isn't an option, I'd store the message in the session or a cookie. When you use URL parameters, the message may appear unexpectedly (reloads, bookmarks, URL sharing, ...).

 

Hmm...I didn't think about that. Good point.

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.