Solved

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

Posted on 2014-09-05
3
277 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 82

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 82

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now