Sandeep590 Posted January 26, 2016 Share Posted January 26, 2016 When iam passing the php variables from one page to another page using session variables , the variables are not posted from one page to another. I have used session_start() method as shown below One page ========= <?php session_start(); $_SESSION['sessionvar'] = $_FILES['FileName']['name']; echo $_SESSION['sessionvar'] ; This is working fine where value is displayed Second page ========== <?php session_start(); if(!empty($_SESSION['sessionvar'])) { $filename = $_SESSION['sessionvar']; echo $filename; } However , $filename is not returning the value and it displays the error message saying that undefined index: $filename. Kindly please help me on this issue. Thanks, Sandeep. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2016 Share Posted January 26, 2016 That is not possible: (1) the index is actually "sessionvar" and (2) you did an empty check just before. What is your real code? Quote Link to comment Share on other sites More sharing options...
Sandeep590 Posted January 26, 2016 Author Share Posted January 26, 2016 The posted one was my original code Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2016 Share Posted January 26, 2016 Then the error message is incorrect. Or the error message is referring to some other code that isn't what you posted. Quote Link to comment Share on other sites More sharing options...
Sandeep590 Posted January 26, 2016 Author Share Posted January 26, 2016 1 warning and 1 notice message were displayed Warning : session_start() : cannot send session cookie - headers already sent by ( output started .............. at line : 405 ) Notice : undefined variable : filename in "Path" at line no : 444 Quote Link to comment Share on other sites More sharing options...
Sandeep590 Posted January 26, 2016 Author Share Posted January 26, 2016 Attached the code snippet and exact error and warning messages Kindly help me session.txt Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted January 26, 2016 Solution Share Posted January 26, 2016 You cannot have output before session_start(). Since HTML markup is output, your script blows up. To avoid this problem now and in the future, I strongly recommend you keep your PHP code and your HTML separate instead of mixing them: All the PHP logic goes to the top of the script, all the HTML markup goes to the bottom. The second problem is that you try to access $filename outside of the if statement which sets this variable. In other words, even when the condition isn't fulfilled and no variable set, you still try to use it. And of course the whole database code is obsolete and wide open to SQL injection attacks. We use PDO with prepared statements now. Quote Link to comment Share on other sites More sharing options...
Sandeep590 Posted January 27, 2016 Author Share Posted January 27, 2016 @Jacques1 , Thank you so much for your valuable explanation on the issue. It worked fine when I placed the session_start() at the top of the page where it is the first line in the page. However, I will look into database code to modify it by using PDO concepts. Just for viewers , to understand the issue and how it got resolved 1) As suggested above , when your file contains a mix of PHP and HTML code, it is always preferred to use the PHP code ahead of the HTML code in order for code to work fine 2) Session_start() should be the first line in the page when you are using session variables to pass the data through variables from one page to another page Thanks once again 1 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.