Solved

IE ignoring my header() request at the end of a script!!

Posted on 2010-08-23
25
438 Views
Last Modified: 2013-12-13
I'm using PHP function which deletes a directory and it's contents.  If the directory is successfully deleted, a header("Location:.......") is called, and if it is not successfully deleted, a slightly different header("Location:....") is called.

This is working absolutely fine in Firefox, Safari & Chrome on a Mac and Firefox on a PC, but not on Internet Explorer.  

IE is deleting the file and it's directory, it just isn't doing the header bit at the end (both are being ignored - it just seems to do nothing!).  Both header() functions re-display the page that the user pressed the 'delete' button on - they are either presented with a fresh file upload mechanism or a message saying that the delete didn't work.

I would be most grateful if anyone could help me solve this - I've been trying to figure it out for hours and it's getting very urgent.
0
Comment
Question by:Tocacar
  • 14
  • 7
  • 4
25 Comments
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499611
You need to make sure that when you call 'header()' that nothing has been written to that page, you need to have no 'echo()' or 'print()' tags before it, otherwise it wont work.
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499623
Nothing is printed before the header is called.  When the user clicks the delete button, a processing page is called which saves some text to a database then calls the function to delete the directory, then the header is called and the page is (supposed to be) displayed.  As I said, it's working fine in every browser except IE.
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33499625
Off the cuff that makes no sense because PHP is a server side script not a client side one...

You are doing the redirect before it even gets to the user.
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499629
show us the redirect script.
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33499634
Do you have a page we can look at or some code perhaps? Which version of ie are you trying in...or is it all of them?
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499640
Thanks everyone for your very fast response to this problem.  The script is as follows (which I found using google and modified slightly to suit my needs):

function purge($dir, $DeleteMe = TRUE)

 {
 
       if ( ! $dh = @opendir ( $dir ) ) return;
      while ( false !== ( $obj = readdir ( $dh ) ) )
      {
            if ( $obj == '.' || $obj == '..') continue;
            if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
      }
      
      closedir ( $dh );
      if ( $DeleteMe )
      {
            if (@rmdir($dir))
            {
                    header("Location:app_page4.php?recid=".$_SESSION["logged_recID"]."&token=".$_SESSION["logged_user_token"]."&file=deleted");
                    
                    } else {
                    
                    header("Location:app_page4.php?recid=".$_SESSION["logged_recID"]."&token=".$_SESSION["logged_user_token"]."&file=notdeleted");
                    
            }
      }
}
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499648
@Snarfles
It's IE v8 that I'm currently using, but I need it to work in 6 as well (I haven't tested that yet - thought I'd get it going on 8 first).  It isn't the end of the world if it doesn't work on 6, but it's definitely got to work on 8.
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499652
Theres a bug in IE v5.5 that wont allow header requests to be submitted to the browser in certain cicumstances. Upgrade your browser and it should work.

Otherwise you a different script to redirect. Such as the one below.
if ($success)    
{    
   echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';    
   exit;    
}    
else    
{    
   echo '<META HTTP-EQUIV="Refresh" Content="0; URL=retry.php">';    
   exit;    
}    

Open in new window

0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499670
@grantunwin
Thanks for the suggestion - unfortunately, it doesn't work.  All I get is the same blank page (which is the processing page called by the user's click of the delete button).
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499687
Change the way you refresh the page like this:
function purge($dir, $DeleteMe = TRUE)

 {
 
       if ( ! $dh = @opendir ( $dir ) ) return;
      while ( false !== ( $obj = readdir ( $dh ) ) )
      {
            if ( $obj == '.' || $obj == '..') continue;
            if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
      }
      
      closedir ( $dh );
      if ( $DeleteMe )
      {
            if (@rmdir($dir))
            {
                    echo "<META HTTP-EQUIV='Refresh' Content='0; URL=app_page4.php?recid=".$_SESSION['logged_recID']."&token=".$_SESSION['logged_user_token']."&file=deleted'>";    
   exit; 
                    
                    } else {
                    
                    echo "<META HTTP-EQUIV='Refresh' Content='0; URL=app_page4.php?recid=".$_SESSION['logged_recID']."&token=".$_SESSION['logged_user_token']."&file=notdeleted'>";    
   exit; 
                    
            }
      }
}

Open in new window

0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499691
Yup, that's exactly what I did.  It doesn't go to app_page4.php, it just sits on the blank processing page....
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499697
what is the name of the blank page you get? app_page4? there may be an error in another part of your pages.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Author Comment

by:Tocacar
ID: 33499714
The flow is as follows:

app_page4.php contains the delete button the user presses
edit_app4.php is called which saves stuff to the db and calls the purge function which is in
functions_upload.php (which is included at the top of edit_app4.php)

When I click the delete button in IE, the file and directory are correctly deleted, but I'm left looking at a blank screen which the URL shows to be edit_app4.php.  All other browsers should me the correct content of app_page4.php.

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33499733
Have a quick read of this..

http://www2.brighthub.com/adrp2d/computing/windows-platform/60039.html

Might be something in ie8 stopping the redirect because it 'thinks' there is malicious code somewhere.

Can you test in ie7 and see if it works?
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499788
@Snarfles
Thanks for the link.  I tried 'Start without Add-ons' (which I don't think I have anyway) - no difference.  I couldn't see anything else there that might help me solve it.

I just tested it on a different PC using IE6 and it's the exact same problem....  Grrrrr.
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33499797
This is bizarre Tocacar...
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499815
Incase it's useful - I've uploaded the 3 pages concerned (apologies for the poor standard of code - I'm not very experienced).
app-page4.php
edit-app4.php
functions-upload.php
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499834
I should probably also say that the header of the page (the HTML bit) is automatically put in place when the page loads by a function in the 'functions.php' include.  It just sets up the HTML page properly and puts out the main navigation bar along with an appropriate text heading for the page.
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33499873
Have you turnede eror reporting on on your server. There may be a simple syntax error somewhere.

Try stoping friendly error messages in IE like this:

    * Internet Explorer > Tools menu > Internet Options > Advanced Tab > Browsing
    * Uncheck the "Show Friendly Error Messages" option.
    * (A better name for it would be: "Hide useful error messages".)

See if that throws out an error description.
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33499906
@grantunwin
My php.ini file is set to:  E_ALL & ~E_DEPRECATED  (should I change this to something else?  I am getting notice's and other errors displayed for other pages).

I just tried the friendly error messages thing in IE - no change I'm afraid.  I tried it a few times and emptied the cache in between attempts just to make fully sure.
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33645074
I've been away for a few weeks, but I'm still trying to solve this problem...

Do you think it could be that Internet Explorer doesn't like the combination of quotes in the header function, and so does nothing:

header("Location:app_page4.php?recid=".$_SESSION["logged_recID"]."&token=".$_SESSION["logged_user_token"]."&file=deleted");

Looking at the manual, header should look like:

header("Location:somelocation");

In my header, my opening quote ends after the equals sign (=) following recid.  Does this mean the header location ends at the equals sign as far as PHP is concerned?  

I can't figure out a way to correctly nest the quotes to test it because of the quotes required in the $_SESSION variables.  I tried using \ before the double quotes after the equals signs but it doesn't seem to be correct by the time I reach the end of the line.  

Any further help/ advice would be most appreciated.  Thank you.
0
 
LVL 3

Expert Comment

by:grantunwin
ID: 33649784
Maybe try using single quotes(') instead of doubles (")within the session variables within you header request. Also, can you show us the live page where your having problems?
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33650049
Thanks for replying grantunwin.  I've already tried just changing the doubles to singles within the session variables, but no improvements.  I was thinking maybe it was the doubles near the equals signs that were the problem(?).

Unfortunately (or fortunately, depending on how you look at it), the form isn't live yet - I really need to get it fixed before it goes live (end Sept) as I'm sure thousands of people will be using IE to complete it.  Hopefully Firefox has taken over as the browser of choice for PC users now....?

It's really annoying, because it worked this time last year on IE.  I was very surprised when I re-hosted the pages this year to find that it wasn't now working (absolutely no changes have been made to the web server configuration between now and then).

I'm stumped.
0
 
LVL 8

Author Comment

by:Tocacar
ID: 33672605
In case anyone is still interested in following this thread, I spent a few hours yesterday echoing out statements in the 'purge' function (see functions_upload.php attachment above) to see where the script was at each different stage when executed using Internet Explorer.   I also changed the call to the purge function slightly (see edit_app4.php above) to receive a return value from purge and, depending on that value, execute the appropriate header call from there, instead of within purge.

IE preferred the return value being executed in the calling code, rather than in the purge statement.  It was now taking me back to the app-page4.php page after I opted to delete the uploaded file instead of presenting me with a blank edit-app4.php page, BUT it was always showing me that the file and it's directory had not been deleted when in fact it had.

Echoing out statements in purge allowed me to see that IE was correctly going through the while loop to recursively check the contents of the directory, and it was deleting the file when it reached it (why does it always find '.' and '..' first?), and then it would delete the directory.  HOWEVER, for some inexplicable reason, it would then return to the top of the function and, again, try to open the directory it had just deleted at which point it would fail, hence it would transport me back to app-page4.php and say that the file had not been deleted when in fact it had.

I persisted with creating echo statements at various points in the purge function and eventually got IE to correctly execute the request to the delete the file (and re-display the correct content of app-page4.php).  Delighted, I carefully commented out the debugging echo statements and IE then promptly stopped running the function correctly(!!).  When I reinstated the echo statements and eventually got it working again, uncommenting them made it stop working.  It is totally bizarre.  

I am going to try a different method of identifying the file within the directory (there is always only one file, but for some reason PHP finds '.' and '..' in the directory as well) and deleting it that way, rather than recursively with the while loop.  Initial tests show that IE still returns to the top of the function to re-open the directory after it has deleted it.  I cannot figure out how or why it is doing this.  When I find out, I will post it here.
0
 
LVL 8

Accepted Solution

by:
Tocacar earned 0 total points
ID: 33702348
OK, it's fixed, but I didn't get to understand why it was doing what it was.  In the end, I drastically simplified the way I was deleting files and dumped the purge function all together.  I now have the following code portion in edit-page4.php which deletes the file in all browsers, including IE.

Thank you to everyone who helped.


if (isset($_POST['action']) && $_POST['action'] == 'delete')  {

	

	

	$dir = '/Library/WebServer/upload/' . $refno . '___' . $filedir . '/';

	

		

	$cv = scandir($dir, 1);

	



	unlink($dir . $cv[0]);

	

	if (file_exists( $dir . $cv[0]))

	{

				

		header("Location:app_page4.php?recid=".$_SESSION["logged_recID"]."&token=".$_SESSION["logged_user_token"]."&file=notdeleted");

		exit;

	

	}	

	

			

	rmdir($dir);

	

	if (!is_dir($dir))

	{

		header("Location:app_page4.php?recid=".$_SESSION['logged_recID']."&token=".$_SESSION['logged_user_token']."&file=deleted");

		exit;

	

	} else {

	

		header("Location:app_page4.php?recid=".$_SESSION["logged_recID"]."&token=".$_SESSION["logged_user_token"]."&file=notdeleted");

		exit;

	}	

	





			

}	

			

Open in new window

0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now