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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

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

Post 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]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

How bout a query string in your form action...

Code: Select all

action="$_SERVER['PHP_SELF']?username=$username"
Or some such thing.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I believe using session would be the best solution for you.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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().
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Two questions: have you passed the username to the session variable, and have you started the session using session_start() ?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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:)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
Post Reply