Results 1 to 9 of 9

Thread: Passing a parameter throughout site / Session variables

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    4

    Question Passing a parameter throughout site / Session variables

    Hi,

    I wonder if anyone can help or has any ideas. I am using NOF 10 and have linked my site to a MySQL database and i'm using PHP. I am trying to create a site whereby once a user has logged in that the user can visit any page and the username is still retained so that the site can pull up details of the user. What I am finding is the user logs in, and the following page will validate and acknowledge correctly that the user has been found, however, what I seem then unable to do is pass the username to another page(s). I've followed the NOF tutorial for a dynamic site/database, i've been through the user guide and everything seems to point to the fact that I somehow need to code into the site the use of session variables.

    So the two questions i have are :
    1) Am I making sense - is what I'm saying above well known and is this the case or have i missed something simple in NOF10 that would allow me to pass a username (once validated within a form against a MySQL record) to only an immediate page that validates the username;

    2) If I was to use session start code for example - does anyone have any examples of how i find the right place to insert the code and what the line might look like that i need to use to assign the form.username variable to a session variable ?

    Any assistance / guidance is really appreciated as I have been trying to solve this one for days and i've read books, user guides, trawled the web and i'm a bit stuck... i'm not a coder by trade - although a long long time ago I used to be (cobol) so i have an idea of what the outcome needs to look like, i just don't know the step to get there in linking NOF code to PHP constructs.

    Thanks in advance.

    James.

  2. #2
    Senior Member LBA's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    543

    Default

    James, the session start code needs to be the VERY first thing on the page. Nothing can come before it. Use insert HTML to do that. After you make sure you've done that, you set the session variable to be equal to the username, or the user id or something like that. Once that's done, the session variable is available to all other site pages... as long as they are PHP pages with session start at the beginning of the page. Tell us if you need guidance on how to insert that code.

    Laurence

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    4

    Default

    Hi Laurence,

    Many thanks for your reply, its extremely appreciated. Ok, i've been battling a little bit more and i've taken a big step back and decided to start with a vanilla 2 page site - simply to see if i can pass a parameter from 1 page to another as part of a session. It sounds so simple and i figured if i can do the simple thing then it will be easy to embed the right chunks of code into the site i'm working on. To that avail, i'm still struggling with the simplest of simple sites. So, I've included the code I've added in manually to the two web pages and wondered if yourself Laurence or anyone else can maybe point out the error of my ways ??

    Ok the outcome i'm looking to achieve is to set a parameter/variable with a value in page 1, and display that parameter/variable in page 2. When I publish the site, I have no page errors, however, nothing is displayed to the screen

    In page 1 I have inserted the following code right at the top, before any NOF headers

    <?php
    function PIPHP_CreateSession($myhandle, $myname, $mycountry)
    {
    if (!session_start()) return FALSE ;

    $_SESSION['myhandle'] = $myhandle ;
    $_SESSION['myname'] = $myname ;
    $_SESSION['mycountry'] = $mycountry ;
    return TRUE ;
    }
    ?>

    <!-- [BEGIN Page Start-of-File Script] -->
    <!-- [END Page Start-of-File Script] -->
    <!-- [BEGIN Master Start-of-File Script] -->
    .
    .
    .

    Then, further down on page 1 within the body of the code I have created a call to the function to PIPHP_CreateSession, using the following code which you'll see is just below the NOF standard insertions :

    <HEAD>
    <TITLE>
    Home
    </TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
    <META NAME="Generator" CONTENT="NetObjects Fusion 10 for Windows">
    <!-- [BEGIN Custom3] -->

    <?php
    $myhandle = "my username" ;
    $myname = "my name" ;
    $mycountry = "my country" ;
    $result = PIPHP_CreateSession($myhandle, $myname, $mycountry) ;
    ?>

    <!-- [END Custom3] -->
    <!-- [BEGIN Layout script] -->

    Then on page 1 all i have is a GIF file which i've defined as a hotspot to jump to page 2...

    One page 2, I have inserted the following code at the very top of the code base/page (again before any NOF standard headers) :

    <?php
    function PIPHP_OpenSession()
    {
    if(!@session_start()) return array(FALSE) ;
    if(!isset($_SESSION['myhandle'])) return array(FALSE) ;
    $vars = array() ;
    $vars[] = $_SESSION['myhandle'] ;
    $vars[] = $_SESSION['myname'] ;
    $vars[] = $_SESSION['mycountry'] ;
    return array(TRUE, $vars) ;
    }
    ?>

    <!-- [BEGIN Page Start-of-File Script] -->

    <!-- [END Page Start-of-File Script] -->
    <!-- [BEGIN Master Start-of-File Script] -->
    .
    .
    ....

    And then further on down within the HTML i've inserted the following code (again you can see the relation of where i have placed the code to the NOF formatting HTML for the page characteristics) :


    ?php
    $result = PIPHP_OpenSession() ;

    if (!$result[0]) echo "Could not open session." ;
    else list ($myhandle, $myname, $mycountry) = $result[1] ;

    echo "This is a Test : " $myhandle ;
    ?>

    <!-- [END Text1] -->
    </TD>
    </TR>
    </TABLE>
    <!-- [BEGIN Page End-of-Body Script] -->

    <!-- [END Page End-of-Body Script] -->
    <!-- [BEGIN Master End-of-Body Script] -->

    <!-- [END Master End-of-Body Script] -->
    </TD>
    </TR>
    </TABLE>
    </DIV>
    </BODY>

    Unfortunately the code isn't displaying any values and so therefore i have no way of knowing if it is working... ultimately what i'd like to get to is a state where i can create a session, and then assign a couple of values which i somehow need to take from NOF and forms (I suspect i might have to manually code the forms piece in PHP too) so that I can pass the parameters into $myhandle, $myname etc so that I can then use them on other pages... ultimately if I can get $myhandle to retain a value then i will use a MySQL table to search for the appropriate record and suck in the users details....

    At this stage it seems like a million years away and this thing is dogging me a little, however, I am sure with any help people can give, that I/we can crack it... I'm doing my best to find a resolution but clearly my best is not good enough...

    Help anyone . Thanks again in advance....

  4. #4
    Senior Member Thomas Frei-Herrmann's Avatar
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    1,000

    Default

    ... funny - just yesterday I have done something similar. In my case I am using the database information in an e-mail. Instead of the e-mail page you use and "echo" command and you've got it.

    Maybe you can read it.

    http://www.tommyherrmanndesign.com/nof/MySQL/html/ausdrucken.html#mail

    ... you need to read the database fields first - on my page:

    $_SESSION['myvariable'] = $database-group-name->fields["name"];

    here is what I have done:

    insert in layout-html "top of the page" on each of your 'session' pages

    Code:
    <?php
    
          @session_start();
    
    ?>
    insert after database connection in the html editor of a textfield on that same page

    Code:
    <?php
    
          $_SESSION['myvariable']  = $database-group-name->fields["name"];
    
    ?>
    insert on your pages to view these SESSION variables in the html editor of a textfield

    Code:
    <?php
    
           echo $_SESSION['myvariable'];
    
    ?>

    Last edited by Thomas Frei-Herrmann; 01-29-2011 at 03:46 PM. Reason: mistake in SESSION, thanks LBA
    Tommy

    Administrator at the German NOF-Forum

    NOF-Tutorials.com | German NOF-Forum | Overview of my tutorials | Facebook

  5. #5
    Senior Member LBA's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    543

    Default

    Thomas' advice is good. Start with a clean 2-page site. Put this at the VERY top of both pages...

    <?php
    session_start();
    ?>

    On the first page, anywhere other than the very top, put this...

    <?php
    $_SESSION['test'] = "It works.";
    ?>

    On the second page, anywhere other than the very top, put this...

    <?php
    echo $_SESSION['test']";
    ?>

    Once you get that going, build up from there.

    Laurence

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    4

    Default

    Hi Guys,

    It's testing my patience... I've done/tried exactly what you said but it never displays on the subsequent page the variable... What am I doing around ? Any ideas ? I've even checked my internet explorer settings to see if I had disabled cookies or something but i haven't... Anyhow, this is what the code looks like :

    Home Page :


    <?php
    session_start();
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Home</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta name="Generator" content="NetObjects Fusion 10 for Windows">
    <link rel="stylesheet" type="text/css" href="./html/style.css">
    <link rel="stylesheet" type="text/css" href="./html/site.css">
    </head>
    <?php
    $_SESSION['test'] = "It works.";
    ?>
    <body style="background-color: rgb(255,255,255); background-image: none; margin: 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="400">
    <tr valign="top" align="left">
    <td width="307" height="201"><img src="./assets/images/autogen/clearpixel.gif" width="307" height="1" border="0" alt=""></td>
    <td></td>
    </tr>
    <tr valign="top" align="left">
    <td height="27"></td>
    <td width="93"><img id="Picture1" height="27" width="93" src="./assets/images/MyButton.gif" border="0" alt="" usemap="#map0"><map name="map0"><area shape="rect" alt="" coords="0,2,93,27" href="./html/page2.html"></map></td>
    </tr>
    </table>
    </body>
    </html>


    And then the code below is the code for the second page....

    <?php
    session_start();
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Page2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta name="Generator" content="NetObjects Fusion 10 for Windows">
    <link rel="stylesheet" type="text/css" href="../html/style.css">
    <link rel="stylesheet" type="text/css" href="../html/site.css">
    </head>
    <body style="background-color: rgb(255,255,255); background-image: none; margin: 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="454">
    <tr valign="top" align="left">
    <td width="326" height="168"><img src="../assets/images/autogen/clearpixel.gif" width="326" height="1" border="0" alt=""></td>
    <td width="128"><img src="../assets/images/autogen/clearpixel.gif" width="128" height="1" border="0" alt=""></td>
    </tr>
    <tr valign="top" align="left">
    <td></td>
    <td width="128" class="TextObject">
    <p>Some text</p>
    <?php
    echo $_SESSION['test'];
    ?>
    </td>
    </tr>
    </table>
    </body>
    </html>

    I get to page two from the home page by simply clicking on a button-image (gif) which i've defined a hot-spot for as this seemed to be the simplest method...

    Any ideas at all ???? Am I doing something really really silly ??

    Sorry for keep asking guys for something that should be so simple, but, the books and advice all seem to be failing me...

    Cheers,

    James.

  7. #7
    Senior Member LBA's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    543

    Default

    It looks like page 2 of your test is named "page2.html". Can't do that. Your PHP code is being ignored. Any page that contains PHP code MUST be renamed to have ".php" as the filename extension. So you must rename page 1 AND page 2 to both have .php as the filename extension... otherwise, your php code does not get executed. In the Properties Palette in Page View, click Custom Names and then choose PHP from the dropdown list.

    Laurence

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    4

    Default

    Hi Laurence, and Thomas,

    Guys guys guys ! Thank You ! Thank You so much for all your valued assistance ! You have helped me massively and I now have this working ! Not only did the information from Laurence/Thomas's above help me pass the parameters but then i applied the code to my development site along with Thomas's code above regarding assigning the database record field to the session variable and hey presto it works !! Guys, when I am next in California and Berlin I would seriously like to buy you guys a beer ! I probably get to both locations at least once a year with my field of work so somehow (hopefully) i can drop you an email through the forum you'll allow me the honour of buying you guys (and partners) a beer... I am so happy I can't contain my excitement as I can now get on with my project as this issue has perplexed me for a couple of weeks.

    Anyhow, once again guys - Thank You.

    Best regards,

    James.

  9. #9
    Senior Member Thomas Frei-Herrmann's Avatar
    Join Date
    Apr 2010
    Location
    Berlin, Germany
    Posts
    1,000

    Default



    OK - this sounds very good

    Attachment 785



    Tommy

    Administrator at the German NOF-Forum

    NOF-Tutorials.com | German NOF-Forum | Overview of my tutorials | Facebook

Tags for this Thread

Posting Permissions

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