SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 33
  1. #1
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)

    start_session() doesn't work in IE6, but works in other browsers?

    I have a bit of code that uses a session to store a search term that a user types into a textfield in a form. This is so the user can navigate the pages of search results, rather than having the page revert back to displaying all results when the page number is changed.

    Here's the code, which works in IE7, opera 9, firefox 3, but not IE6.

    Code:
    /* remove everything but letters, numbers, spaces, - and _ */
    	function sanatize_search($raw){
    		return preg_replace('![^A-Za-z0-9_-\s^\t^\n^\r]!', "", $raw);
    	}
    
    $var_search="";
    
    
    //get search terms
    	session_start();
    
    	if(isset($_POST['submit_clear_search'])){
    		if(isset($_SESSION['stored_search'])){
    			unset($_SESSION['stored_search']);
    		}
    	}
    	elseif(isset($_POST['submit_search'])){
    		$var_search = sanatize_search($_POST['tb_search']);
    		$_SESSION['stored_search'] = $var_search;
    	}
    	elseif(isset($_SESSION['stored_search'])){
    			$var_search = $_SESSION['stored_search'];
    	}
    	
    	//end get search terms

    Any ideas? I've never heard of such a thing before


    Thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Put session_start() at the top of the script

  3. #3
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,782
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Hi

    session_start() has to happen before ANYTHING is given to the browser. It is a header and must come first RIGHT after <?php

    Cheers.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RyanReese View Post
    Hi

    session_start() has to happen before ANYTHING is given to the browser. It is a header and must come first RIGHT after <?php

    Cheers.
    The OP's existing code doesn't appear to send anything to the browser before session_start is called. It just declares a function and initializes a variable. If he was sending something to the browser first, it wouldn't work in any browser.

  5. #5
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    What I posted was only a code snippit of the relevant code.

    There are several function and variables preceeding the snippit, as well as a few require_once statements to other php files.

    None of them have any output until the start of the html tag. There are no extra lines outside of the php tags.

    However, there are a number of php tags starting and stopping, which would appear like this:

    Code:
    <?php
    
    /* stuff here */
    
    ?>
    <? php
    
    /* more stuff */
    
    ?>
    No spaces or returns outside, except for when a tag starts or stops. Would that be causing the issue?

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    What I posted was only a code snippit of the relevant code.

    There are several function and variables preceeding the snippit, as well as a few require_once statements to other php files.

    None of them have any output until the start of the html tag. There are no extra lines outside of the php tags.

    However, there are a number of php tags starting and stopping, which would appear like this:

    Code:
    <?php
    
    /* stuff here */
    
    ?>
    <? php
    
    /* more stuff */
    
    ?>
    No spaces or returns outside, except for when a tag starts or stops. Would that be causing the issue?
    Not sure, but it may be possible that the newline characters after each ?> tag are tripping you up.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    Not sure, but it may be possible that the newline characters after each ?> tag are tripping you up.
    No, php strips those. If there was output before session_start, there would be a very specific warning about this. Since it happens server side, it would be the same for all browsers.

    Just in case - Make sure that warnings are displayed. Put this in the beginning of your script:
    PHP Code:
    error_reporting(E_ALL); 
    Force Flow: Do you have cookies disabled for your IE browser?

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    Not sure, but it may be possible that the newline characters after each ?> tag are tripping you up.
    Yes, who knows. Put echo_start in the first line of the script, and see what happens. If it still doesn't work, at least you'll have eliminated one possible cause

  9. #9
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    
    /* stuff here */
    
    ?>HERE
    <? php
    
    /* more stuff */
    
    ?>
    There should be no space between, not even a carriage return/newline/whitespace. Alternatively you could use ob_start which will queue content until after headers are sent.
    Cross browser css bugs

    Dan Schulz you will be missed

  10. #10
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Alternatively you could use ob_start which will queue content until after headers are sent.
    Excellent idea. This will completely eliminate whitespace as the culprit.

  11. #11
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    This issue occurs on two seperate workstations (the only remaining workstations I have with IE6)

    error reporting is on, and no errors on the page or in the logs.

    Cookies are enabled.

    what is "echo_start"? I don't see it on PHP.net as a valid function.

  12. #12
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Code:
    <?php
    
    /* stuff here */
    
    ?>HERE
    <? php
    
    /* more stuff */
    
    ?>
    There should be no space between, not even a carriage return/newline/whitespace. Alternatively you could use ob_start which will queue content until after headers are sent.
    What about when other files are access with require or include statements?

    [edit]: I tried using ob_start() and ob_end_flush() to surround the portion of the code where session is being used, but that didn't work. I also tried using ob_start() at the start of the file and ob_end_flush() after the session code. that didn't work either

  13. #13
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If those files being included/required are outputting content before Headers, it will always give you that Headers already sent error. Either eliminate all output in the include files, or put ob_start at the top so you don't have to worry as much.
    Cross browser css bugs

    Dan Schulz you will be missed

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Force Flow View Post
    what is "echo_start"? I don't see it on PHP.net as a valid function.
    I meant session_start

  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do some debugging. Focus on testing each tiny individual aspect of it. Understand what your code is actually doing at runtime, because it obviously isnt what you expect.
    PHP Code:

        session_start
    ();
    // did the browser send a session cookie? lets look
    print_r($_COOKIE);

    // lets see what variables and values were sent
    print_r($_POST);

    // lets see whats in the session right now
    print_r($_SESSION);

        if(isset(
    $_POST['submit_clear_search'])){
            echo 
    'post submit_clear_search was set';
            if(isset(
    $_SESSION['stored_search'])){
                echo 
    'session stored_search was set';
                unset(
    $_SESSION['stored_search']);
            }
        }
        elseif(isset(
    $_POST['submit_search'])){
            
    $var_search sanatize_search($_POST['tb_search']);
            
    $_SESSION['stored_search'] = $var_search;
        }
        elseif(isset(
    $_SESSION['stored_search'])){
                
    $var_search $_SESSION['stored_search'];
        } 

  16. #16
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Well this is interesting...

    IE:

    Before search:
    Code:
    Array
    (
        [PHPSESSID] => 709f165a1ca7e2a7b461e5115d7c0c62
    )
    Array
    (
    )
    Array
    (
    )
    After search:
    Code:
    Array
    (
        [PHPSESSID] => 709f165a1ca7e2a7b461e5115d7c0c62
    )
    Array
    (
        [tb_search] => whatityped
        [submit_search] => Search
        [submit_clear_search] => Clear&nbsp;Search
    )
    Array
    (
    )


    Firefox/opera/IE7:

    Before search:
    Code:
    Array
    (
        [PHPSESSID] => 72bd32362d053a6187b07c85767206f0
    )
    Array
    (
    )
    Array
    (
    )

    After search:

    Code:
    Array
    (
        [PHPSESSID] => 72bd32362d053a6187b07c85767206f0
    )
    Array
    (
        [tb_search] => whatityped
        [submit_search] => 
    )
    Array
    (
    )

    There are two form submit buttons. One is to process the search, the other is to clear it. In IE6, the values for both appear. All the other browsers don't have that issue.

    So...this issue is actually caused by there being one button (button element, not input element) that does the search and the other that removes it? does isset() not detect which button was pressed?

    Code:
    <div id="area_search">
    	<form id="search" action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
    		<div>
    			<input id="tb_search" name="tb_search" type="text" size="40" maxlength="250" value="<?php echo $var_search ?>" />
    			<button type="submit" name="submit_search" onmouseover="this.className='button_hover_positive'" onmouseout="this.className='button'">Search</button>&nbsp;&nbsp;<button type="submit" name="submit_clear_search" onmouseover="this.className='button_hover_negative'" onmouseout="this.className='button'">Clear&nbsp;Search</button>
    		</div>
    	</form>
    </div>

  17. #17
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,782
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    It does as long as the name="" is specified on that particular element. You should be using <input>'s and not buttons: at least until we get this sorted out. IE is a bit weird about <button>'s in the first place anyway.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  18. #18
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI, it's a known issue that button elements are buggy in IE (especially multiple buttons) - I believe they send the innerText/innerHTML and not only the value. Try changing them to inputs.
    Cross browser css bugs

    Dan Schulz you will be missed

  19. #19
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Drat. If there was a quick "hack" for this to let me still use the button element, I'd use it, but as things stand, I don't want to bother to redo all the form CSS.

    As much as I like to have webapps work across all browsers, since this thing is for limited use, rather than public use, I'll drop IE6 support for now and add a little notice for the IE6 conditional CSS.

    So..here's the idea if anyone's interested, though this is a bad idea for regular websites:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style type="text/css">
    	body, div, span, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, button, p, blockquote, table, th, td { margin:0px; padding:0px; }
    	body {
    		font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
    		font-size: 100.01&#37;;
    		color: black;
    	}
    	p { margin: .2em 0 .8em 0; }
    	#browserwarning {
    		display:none;
    	}
    </style>
    <!--[if lt IE 7]>
    	<style type-"text/css">
    		#browserwarning {
    			display:block;
    			position: absolute;
    			bottom: 0;
    			left: 0;
    			font-style: italic;
    			color: #666;
    			border: 1px #d4d5aa solid;
    			border-right-color:#999;
    			border-bottom-color:#999;
    			background-color: #FFFBC6;
    			padding: 5px 8px 0px 8px;
    		}
    	</style>
    <![endif]-->
    
    </head>
    
    <body>
    
    <p>blah blah</p>
    
    <div id="browserwarning"><p><strong>Notice:</strong> This application does not fully support your browser (IE6) and functionality will be affected. Please upgrade or use an alternative browser.</p></div>
    
    </body>
    
    </html>

  20. #20
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cannot reproduce IE7 sending name/value pairs for both submit buttons on the same form submission. That seems like a pretty terrible bug that can probably only be worked around by not using multiple submit buttons, or by using scripting.

    Try setting a value="something" attribute for your <button> elements. In IE7, only the name is submitted if I omit that attribute, while in your version of IE it seems to use the elements contents as the value. Maybe IE thinks it should send both because of this.

    Why do you even need a "clear search" option? Why would a user need to clear it? To help your php script save memory/overhead by taking it out of the session? I would think keep the data, and overwrite it when a new search is performed.

    Despite this, your current method is not very good in any case. Users often submit forms using thier enter key. Most browsers will send the name/value of the submit button which appears textually first in the forms html, but some browsers simply won't send any name/value pair for any of the submit buttons at all. I think it's logical that you should assume the first submit button was pressed in the absence of any name/value pairs sent to you, as it's the behavior the majority of browsers use.

  21. #21
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    crmalibu, first, you're over analyzing this.

    Second, this is an IE6 issue only because of the broken way button elements are interpreted. IE7 works fine.

    In IE6, the button element looks to the innerHTML rather than the value attribute, and with forms, using multiple button elements as submit buttons sends all the name/value pairs regardless of which button was actually clicked.

    The "proper" solution here appears to use the input button, reset, or submit types, rather than button element to ensure it works with everything and IE6. Coming up with javascript to handle the shortcoming is not a good idea because it's a waste of time (for me anyway), and it's dependant on javascript being enabled.


    The "clear search" button is there so the user won't have to close the page or revisit a different page in order to clear it. There's a dual purpose for retaining the search result...navigating the pages of results and showing the user in the UI what the search term(s) were.

    I know of a few CMS systems that operate in a similar fashion from a UI standpoint (joomla is one of them).

    Memory/overhead is negligent. Hitting the enter key defaults to search, not clear.

    Most of the items you brought up are non-issues.

  22. #22
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ...and there was me thinking you came here asking for advice...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  23. #23
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Hitting the enter key defaults to search, not clear.
    If the browser doesn't send a variable named submit_search, the code you posted will result in $var_search=""; I'm guessing this means your code wont search.

  24. #24
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    ...and there was me thinking you came here asking for advice...
    I did, however, crmalibu brought up items unrelated to the issue and I was just attempting to address those comments very briefly.


    Quote Originally Posted by crmalibu View Post
    If the browser doesn't send a variable named submit_search, the code you posted will result in $var_search=""; I'm guessing this means your code wont search.
    crmalibu, again, not quite the issue. What happens in IE6 with button elements is that the name/innerHTML pairs for all submit type button elements in a form are POSTed. As a result, the conditional statements do not operate correctly because isset() sees not only the submit button that was clicked, but all the other submit buttons as well.

    This is different in that with other browsers, only the button element (that is a submit button type) that is clicked will send a name/value pair. Thus, isset() will only see that one submit button which was clicked.



    Anyway, thanks to everyone for helping to troubleshoot

  25. #25
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My point being, this issue aside, I'm telling you that your conditional statements in your php code still doesn't work properly for some browsers.

    When people come here for help, and we see bad code and bad ways to approach a solution, most of us point it out even if its not 101&#37; directly related to someones issue. But you seem to think you know better.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •