PHP $_SESSION passing variable to another page-help with syntax

Hi

I would like to store a userID into $_SESSION then reference the $_SESSION in another page. I get how the Session function supposed to work but need help with the syntax please?

I have  a table called users which stores uid, username and password

I have then started the session function like this

<?php

class DB_Functions {
session_start();

Then tried to use the $_SESSION here:

 /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {
        $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
        // check for result
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $_SESSION[email] =$email
            $result = mysql_fetch_array($result);
            $salt = $result['salt'];
            $encrypted_password = $result['encrypted_password'];
            $hash = $this->checkhashSSHA($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;

Then called it in the next page as below:
<?php

session_start();

$result = mysql_query("SELECT users.uid, users.name
FROM users
WHERE EXISTS (SELECT 0 FROM available A1, available A2
WHERE  users.uid <> $_SESSION[email])") or die(mysql_error());

But got syntax error

How do i store the ID from the user returned in the 1st query then pass to the second query correctly?
wilko1000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Couple of things.  First, you need to get off MySQL as soon as possible.  PHP is removing support for the MySQL extension.  This article tells why and what you must do to keep your scripts running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Second, the PHP session is really easy to use correctly, and many people over-think it.  This article tells what you want to do.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

Looking at this code snippet, it appears that it would fail, since you cannot have function calls outside of class methods.  Session_start() does not belong here.
class DB_Functions {
session_start();

Open in new window

You may have had some of the issues because of the use of quotation marks in PHP.  They are required in some instances, and using them correctly is an important part of the language.  I'm not sure about that, because PHP has many installation options that can impinge on the use of quotes, but you might want to check the code over to make sure it's coded correctly to do what you want.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

If you can show us where you got the syntax error and what the error message said, we can probably help with that.
0
Dave BaldwinFixer of ProblemsCommented:
'session_start();' belongs at the top of the PHP file before anything but error_reporting and definitely not inside any function or 'if' statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wilko1000Author Commented:
Hi thanks for the feedback

My fault typo
<?php
session_start();

Thats how it looks.

Anyway i still do not know the 'Syntax' articles are great but no examples.
My biggest concern is not be able to use $_SESSION within functions and if statements (with all due respect no mention of this in the articles ).

As my DB functions script is constructed using functions are you saying im unable to use $_SESSION in this script?

[attached for reference ]
DB-Functions.php
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Ray PaseurCommented:
$_SESSION is a superglobal variable.  Please see How the PHP Session Handler Works in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

See also:
http://php.net/manual/en/book.session.php

And this:
http://php.net/manual/en/language.variables.superglobals.php

Superglobal variables are present in every scope and namespace.  You can use them anywhere for read or write, inside open code, inside class methods, inside functions, etc.
0
Dave BaldwinFixer of ProblemsCommented:
You can use $_SESSION variables anywhere you use 'regular' variables IF you run session_start(); first at the top of the page.  session_start(); creates or renews the session identity that is used to access your $_SESSION variables.
1
Mark BradyPrincipal Data EngineerCommented:
In both cases $_SESSION[email] should be $_SESSION['email']

$email is a string so you must reference it as a string value and not a defined value. PHP will consider $_SESSION[email]  (the email part) as a defined value and if it isn't defined anywhere it will give you grief.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.