Solved

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

Posted on 2014-09-05
3
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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