Page 1 of 2

Challenge for php guru - this is making my head ache!

Posted: Tue Mar 22, 2005 1:09 pm
by spacebiscuit
I'm having a trouble with an i-frame and variable passing. I am not sure if what I am attempting is possible but knowing how versatile php is I hopeful.

I am writing an admin system which requires a login. Now the screen if you can picture it, consists of two parts:

- a left hand navigation
- a main right hand side

The main right hand side is an i-frame. I want to use this so that the entire page doesn't have to re-load each time an option is chossen from the left-hand menu.

Now I am using the following sample code in the frame to ask for a username:

Code: Select all

<?php
$username=$_GET['username'];

if (isset($_GET['submit'])){
              
      // if form is submitted to this.....

                      	     }

else{

      ?>
      form action="<? $PHP_SELF; ?>">
      <p class="bodytext">Please input your username.</p>
      <p class=bodytext>
       <input type="text" size="15" name=username
      </p>
      <input class=button type="submit" value="Submit" name="submit">
      </form>
      <?			
                   
      }
?>
The above code works ok and passes the variable 'username' successfully once the form is submitted.

Now the problem is that I have, as I mentioned earlier, a left hand side navigation menu. Now these link to the i-frame as follows:

<a href=main.php?function=profile target=main>...<a>

the variable function is called in the 'main.php' script and as the name suggests, it's the title of a function. In this case the function is called 'profile'.

Again this works perfectly. When I click the link the variable 'function' is passed and my function called 'profile' executes as I want.

Now my problem is that I also want to call my function 'profile' with a paramter - that being the username. The left-hand navigation menu has no reference to the 'username' variable since this is only determined once the form has been submitted within the i-frame. The lefthand side navigation only executs once when the entire script is first called.

I am guessing that I only need to send the 'function' variable (which is static and will not change) but then how do I then somehow reference the 'username' variable, as a global or cookie perhaps?

If anyone could suggest anything or point me in the right direction I would be very much grateful.

Many thanks for reading this much!

Rob.


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Mar 22, 2005 1:15 pm
by neophyte
How bout a query string in your form action...

Code: Select all

action="$_SERVER['PHP_SELF']?username=$username"
Or some such thing.

Posted: Tue Mar 22, 2005 1:18 pm
by Chris Corbyn
So you need the main iframe at the right to pass the username back to the left navigation?

:arrow: JavaScript, unless you set a session variable and redirect the entire page to

$_SERVER['PHP_SELF'].SID

8)

Posted: Tue Mar 22, 2005 1:18 pm
by spacebiscuit
I was using a POST action in the form previously, but removed it to test with the GET method instead. Regardless it doesn't make any difference as the problem is not concerned with the form.

Thanks,

Rob.

Posted: Tue Mar 22, 2005 1:21 pm
by spacebiscuit
I'd rather stick to the php route:

Can you enlighten me in anyway I might use your suggestion:

$_SERVER['PHP_SELF'].SID

Thanks,

Rob.

Posted: Tue Mar 22, 2005 1:22 pm
by Weirdan
I believe using session would be the best solution for you.

Posted: Tue Mar 22, 2005 8:34 pm
by spacebiscuit
Hi,

I decided to try the session variables.

I used this piece of code to register the session variable:

session_register("username");

However when I call the script I am getting this message:

Warning: session_register(): Cannot send session cookie - headers already sent by (output started at c:\inetpub\wwwroot\main.php:10) in c:\inetpub\wwwroot\main.php on line 12

Warning: session_register(): Cannot send session cache limiter - headers already sent (output started at c:\inetpub\wwwroot\main.php:10) in c:\inetpub\wwwroot\main.php on line 12


Any ideas?

Thanks very much.

Rob.

Posted: Tue Mar 22, 2005 8:37 pm
by feyd
viewtopic.php?t=1157

also note:
session_register() wrote:Caution

If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

register_globals: important note: Since PHP 4.2.0, the default value for the PHP directive register_globals is off. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.

Caution

This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.

Caution

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().

Posted: Tue Mar 22, 2005 8:42 pm
by spacebiscuit
Can I just add what a friendly and helpful board this is.

I work from home free-lance often with no one else I can share my php related issues with.

At last I think I have found somewhere.

Cheers guys!

Rob.

Posted: Tue Mar 22, 2005 8:49 pm
by John Cartwright
robburne wrote:Can I just add what a friendly and helpful board this is.

I work from home free-lance often with no one else I can share my php related issues with.

At last I think I have found somewhere.

Cheers guys!

Rob.
Welcome to the boards :) Enjoy your stay

Posted: Tue Mar 22, 2005 9:02 pm
by spacebiscuit
Ok I am gonig to avoid the session_register() route and try the $_SESSION array method.

So, I am using the POST method within a form to send data to the same script - $PHP_SELF

I am then trying to access this data with.....

$username=$_SESSION['username'];

This isn't working. Am I doing something obviously wrong or is there a problem with my php config?

Thanks,

Rob.

Posted: Tue Mar 22, 2005 9:08 pm
by Ambush Commander
Two questions: have you passed the username to the session variable, and have you started the session using session_start() ?

Posted: Tue Mar 22, 2005 9:50 pm
by hongco
robburne wrote:Can I just add what a friendly and helpful board this is.

I work from home free-lance often with no one else I can share my php related issues with.

At last I think I have found somewhere.

Cheers guys!

Rob.
welcome, I like it here too:)

Posted: Tue Mar 22, 2005 10:21 pm
by magicrobotmonkey
Don't forget that you can

Code: Select all

var_dump($_SESSION)
to make sure that what you think is happening actually is.

My personal reccomendation would be to build a user class and register it in the session. It will makes things much easier in the long run, especially if you plan on having permissions or anything like that. You can use it on your login form to accept the user and pass and then use it from your menu to make sure the user is logged in, or whatever you need to do.

Posted: Wed Mar 23, 2005 5:07 am
by spacebiscuit
I will add the sessin start to the script - this needs to be at the very top of the script right?

What is the syntax for passing the 'username' to the session variable?

The username class sound sinteresting but I have no idea.

Rob.