SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session and heredoc

    I've started a session.
    A form on the page posts data to mySQL db
    Page refreshes back to self but displays a "Process Complete" message via heredoc syntax.
    The contents of the heredoc is formatted as an HTML page including a META TAG refresh to a different page.
    This new page (page 3) needs to be part of the session but seems not to be.

    Here is some code;
    PHP Code:
    // page 2
    session_start(); 
    $_SESSION['type_id'] = $type_id// use if first page of session
    echo $_SESSION['type_id'];

    // some query stuff and other stuff here

    // the heredoc with META TAG directing to 3rd page of series
    echo <<<EOD
    <html>
    <head>
        <title>Admin Page</title>
        <META HTTP-EQUIV="Refresh" CONTENT="05; URL=http://www.domainname.com/page3.php">
    </head>
    <body>
        <div id="gallery">
            <p>Process complete!</p />
        </div>
    </body>
    </html>
    EOD;

    The 3rd page is to be part of the session and is to use the session id as a var in a mySQL query
    PHP Code:
    // page 3
     
    session_start();
     
    $type_id $_SESSION['type_id'];
     echo 
    $_SESSION['type_id'];

    $result mysql_query"
            SELECT category_id,category_name,category_type,category_order
            FROM gallery_category
            WHERE category_type='
    $type_id'
            ORDER BY category_order" 
    ); 
    So in the mySQL query, the WHERE category_type is to be the same value as the value of $type_id which is the session id.

    How far off am I? Thanks.


    //

  2. #2
    SitePoint Zealot Skibum1321's Avatar
    Join Date
    Jun 2003
    Location
    Malden, MA
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may be wrong, but I think you just need to get rid of the single quotes around the variable. This will force PHP to interpret it as a string. In double quotes, the variable will be replaced by its value.

    PHP Code:
    $result mysql_query
            SELECT category_id,category_name,category_type,category_order 
            FROM gallery_category 
            WHERE category_type=
    $type_id
            ORDER BY category_order" 
    ); 
    Keith Rousseau

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification. I had used all three in desperation to try to get some result. When I replace the variable $type_id (which is supposed to be the session variable) with one of its possible values, the correct results return.
    PHP Code:
    result mysql_query
    ********SELECT category_id,category_name,category_type,category_order 
    ********FROM gallery_category 
    ********WHERE category_type=1 
    ********ORDER BY category_order" 
    ); 
    This means that the session variable is being lost, right? Also when I try to echo the session variable on page3 nothing returns ...further confirming.

    SO! From the time the user leaves page2 where the session id is set, to page3 it is lost. Between those two moments, the only weird thing that might impact the session (possibly clearing it) is the heredoc message.

    How would I echo the session id within a heredoc?

    Thanks again.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo <<<EOD 
    <html
    <
    head
        <
    title>Admin Page</title
        <
    META HTTP-EQUIV="Refresh" CONTENT="05; URL=http://www.domainname.com/page3.php"
    </
    head
    etc...
    EOD
    should be

    PHP Code:
    // php ends here
    ?>
    <html> 
    <head> 
        <title>Admin Page</title> 
        <META HTTP-EQUIV="Refresh" CONTENT="05; URL=http://www.domainname.com/page3.php?<?=session_name()?>=<?=session_id()?>"> 
    etc...

  5. #5
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skibum1321
    I may be wrong, but I think you just need to get rid of the single quotes around the variable. This will force PHP to interpret it as a string. In double quotes, the variable will be replaced by its value.
    You are right about being wrong.

    Quotes within quotes will just echo out literally.
    PHP Code:
    $a 20;
    echo 
    $a// outputs: 20
    echo '$a'// outputs: $a
    echo "$a"// outputs: 20
    echo "'$a'"// outputs: '20'
    echo '"$a"'// outputs: "$a" 
    However, in this case I think they should be ommited but because as i understand it $type_id is a numerical value.

    One should not quote numerical parameters in in an SQL statement (they are not needed and it will work in mySql only). String paramenters in SQL should always be quoted.

    Rik
    Last edited by Zoef; Jun 4, 2004 at 10:36.
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. Stereofrog's tip answered what I thought was going to be the problem. I'm trying to refresh via META within a heredoc string, so variables will not work.

    Zoef, yes you understand correctly that $type_id is a numerical value. I've found that an attempt at echo of the $_SESSION from within the heredoc is not returned, no matter what I do with quotes.

    This being the case, the session value is cleared and I think it's because I used $_GET['type_id'] for the session value. Like this...
    PHP Code:
    session_start(); 
    $_SESSION['type_id'] = $_GET['type_id'];
    echo 
    $_SESSION['type_id']; // this does echo correctly but it's the last point where I can echo it. 
    Would it be because I set the SESSION var value from the URL? When the URL changes, then the SESSION value does too?


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
  •