Link to home
Start Free TrialLog in
Avatar of wilko1000
wilko1000

asked on

PHP how to use $_Session to transfer a value stored in a username text box to a sql query

Hi
I have an Android app that allows the user to look up books.

The app has login page that queries a PHP script (hosted in wamp) that checks the username and password and if it exists takes you to the main page.
The main page runs a PHP script that contains a SQL script that returns a list users of the app in a listview . Upto now this works ok.

However what i want to do is just return all users but not the user that logged in. I have read that $_Session can be used as a global variable. So I could use $_Session to store the username and then run the query to get all users <> user thats logged in.
Can anyone give me an example or point me to where i can find out how to use $_Session variable for this use?

For example im on the right track using it like this?

users.usertable
FROM users
WHERE EXISTS (SELECT 0 FROM available A1, available A2
WHERE users.uid <>$_Session [username])") or die(mysql_error())
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're new to PHP and want some good learning resources, this article can help.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

$_SESSION (case-sensitive) is a PHP superglobal variable.  Used properly it can provide "stateful" information that is carried by the server between requests.  To fully understand how it works, you must understand the nature of HTTP Client/Server protocols.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

Once you've "got" that, the PHP session is very easy to use correctly!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

Session data can be used in SQL queries, but you may find yourself dealing with fiddly punctuation because of all the quote marks, and there are fairly serious security implications of using external data in a query string.  Recommend you start with the first article and build up your experience in PHP a little at a time.  Or if time does not permit, consider hiring a professional developer to help.