Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GhostDZ9

macrumors regular
Original poster
Sep 13, 2010
118
0
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?
 
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).
 
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.
 
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.
 
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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.