Solved

Can a session variable be accessed on a background php page the user does not see?

Posted on 2014-09-05
3
281 Views
Last Modified: 2014-09-05
if (isset($_SESSION['user']) && $_SESSION['user'] != '') {
  $username = $_SESSION['user'];
}

Open in new window


I have a file upload form. I get a file id number from the url, and use AJAX to send it to a background php page to upload a file. I want that background php page that the user never sees to get the username from a session variable and use that name to cross check the file id from that url by checking the database to verify the file id matches with the username in the same table row.

When I try this, the session variable appears to be empty or it cannot be accessed by a background php page.

My second option would be to send in the url a user id in addition to a file id. Then access the database to be sure the two id's go together. But I think this would be less secure.

1. My question is; Is it true session variables cannot be accessed by background php pages?
2. If yes, then how might I perform a cross check without the session variable? Thanks.
0
Comment
Question by:kadin
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40307221
No, 'background' has nothing to do with it.  I use sessions on 'invisible' pages all the time.  But the page Must Always have session_start() at the top of it.  That's how PHP picks up the current session.  On several pages, I even start the session with an AJAX call.  session_start() is the key.
0
 

Author Closing Comment

by:kadin
ID: 40307230
Thanks. That didn't even occur to me.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40307237
You're welcome, glad to help.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question