PDA

View Full Version : PHP Session Help




GhostDZ9
Nov 4, 2010, 03:08 PM
Hey guys,

I am currently design a Web Based CMS, and I need to figure something out.

I have one file for login form in html, it does the validation of the login in to a php file then it loads another page if it is entered in correctly.

This is the code for the redirection part

"header("location:main.php");

Now what i want to do is pass the user name from the login form to the main.php page so i can display welcome (username)!

can someone help me?



aarond12
Nov 4, 2010, 03:19 PM
You can easily pass values from page to page using hidden input values, populated by your scripts.

Page 1:
<input type="text" name="yourname"/>
[submit the form]

Page 2:
<input type="hidden" name="yourname" value="<?php echo @$_POST["yourname"] ?>"/>

You can always use cookies (which is likely the better choice in this case).

GhostDZ9
Nov 4, 2010, 03:24 PM
i tried it, the page shows up blank

angelwatt
Nov 4, 2010, 07:49 PM
If you're using sessions, then you should be able to just access $_POST['username'] or whatever you named the field for the user name. Just stick this in the session, then you can retrieve it later on the redirected to page. This will work as long as you are using sessions correctly.

joris538
Nov 4, 2010, 08:53 PM
When you redirect a page using header, the POST won't be recognised. It is only valid on the page which redirects to main.php. So what you can do is retrieve the information on main.php from the database, or set $_SESSION['username'] = $_POST['yourname']; on the login page then on main.php requesting $_SESSION['username'] for the welcome username.
What you shouldn't forget is session_start() at the beginning of each page, so PHP knows when to use the sessions.

Zortrium
Nov 5, 2010, 06:03 AM
You can easily pass values from page to page using hidden input values, populated by your scripts.

Page 1:
<input type="text" name="yourname"/>
[submit the form]

Page 2:
<input type="hidden" name="yourname" value="<?php echo @$_POST["yourname"] ?>"/>

You can always use cookies (which is likely the better choice in this case).
Using inputs is a bad idea from a security perspective, since a user can change that to whatever they want and resubmit the page.

As the above poster has noted, using session variables in PHP is dead-easy. Just call session_start() somewhere near the start of your script, then you can access the _SESSION array to store or retrieve whatever you want.

GhostDZ9
Nov 5, 2010, 09:44 AM
Yeah thats what i thought to I have it starting at the beginning of my script but it doesn't seem to access the information. may be i have it starting in the wrong page.

I have the session starting in the main.php page

any ideas?

angelwatt
Nov 6, 2010, 03:31 PM
any ideas?

Provide your code.

GhostDZ9
Nov 8, 2010, 09:51 AM
I actually got it working so I'm ok for now thanks though! :)