arnoldd99 Posted November 17, 2007 Share Posted November 17, 2007 Im having trouble with sessions. I want to create a variable that is avaliable for all pages of my site. The variable is determined by user input from a form. I have managed the first part: $username=$_POST['username']; I then wrote this for my session variable <? session_start(); $username=$_SESSION['username']=$_POST['username']; ?> How do i use this variable on another page. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 17, 2007 Share Posted November 17, 2007 You'd use $_SESSION['username'] to access the username from the session. Make sure you have session_start(); at the top of every page that uses sessions though in order for PHP to retrieve the current session variable. Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 Yeah, try this, load it, then refresh! <?php session_start(); print $_SESSION['username']; $_SESSION['username']="jolly"; ?> Quote Link to comment Share on other sites More sharing options...
arnoldd99 Posted November 17, 2007 Author Share Posted November 17, 2007 You'd use $_SESSION['username'] to access the username from the session. Make sure you have session_start(); at the top of every page that uses sessions though in order for PHP to retrieve the current session variable. I have done this but when i go to echo the variable to check the contents is there nothing appears. Code i used: $_SESSION['username']; $username=$_SESSION['username']; print ($username); Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 From your last post, it looks like you didn't assign anything to the session variable, therefore nothing will be printed. Look again. Quote Link to comment Share on other sites More sharing options...
arnoldd99 Posted November 17, 2007 Author Share Posted November 17, 2007 sorry i wasnt very clear with my last post: I have two pages, the first feeds.php <? session_start(); $username=$_SESSION['username']=$_POST['username']; ?> <?php $username=$_POST['username']; ?> The second page userPage.php <?php session_start(); $_SESSION['username']; $username=$_SESSION['username']; print ($username); ?> Is there still nothing assinged to the session variable? Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 Change the first page to: <? session_start(); print $_POST['username']; $username=$_SESSION['username']=$_POST['username']; ?> <?php $username=$_POST['username']; ?> And check that what you've posted to it is there! Quote Link to comment Share on other sites More sharing options...
arnoldd99 Posted November 17, 2007 Author Share Posted November 17, 2007 When i filled out the form, i entered the user name nick and nick appeared when i clicked ok. However it still doesnt appear on userPage.php Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 Try this: First page. session_start(); $username=$_POST['username']; $_SESSION['username']=$username; UserPage.php session_start(); $username=$_SESSION['username']; print ($username); Quote Link to comment Share on other sites More sharing options...
arnoldd99 Posted November 17, 2007 Author Share Posted November 17, 2007 Still no luck. Here is the code for the first page <?php session_start(); $username=$_POST['username']; $_SESSION['username']=$username; ?> <html> <head> <?php $username=$_POST['username']; $data="$username \n"; </head> <body> <?php $fh=fopen("$username.txt","a"); fwrite($fh,"$data"); fclose($fh); print("Details sumbitted"); ?> <form action="userPage.php" method="POST" /> <input type="submit" value="click here to continue" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 17, 2007 Share Posted November 17, 2007 Have you turned on error reporting in your php files, there must be some header errors in your previous code. Try this one... <?php session_start(); // session start must be top of the page error_reporting(E_ALL); // this line added $username=$_POST['username']; $_SESSION['username']=$username; // you dont have to open multiple php tags 1 is ok if($_SERVER['REQUEST_METHOD'] == 'POST') { $username=$_POST['username']; $data="$username \n"; $fh=fopen("$username.txt","a"); fwrite($fh,"$data"); fclose($fh); print("Details sumbitted"); } ?> <html> <head> <form action="userPage.php" method="POST" /> <input type="submit" value="click here to continue" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.