Jump to content

Issue with session variables


Go to solution Solved by Jacques1,

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/300669-issue-with-session-variables/
Share on other sites

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

  • Solution

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.

@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
 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.