1
if(isset($_POST['submit']))
        {
            $username=$_POST['username'];
            $password = md5($_POST['pwd']);
            $query="SELECT * FROM users WHERE username='".$username."' AND pwd='".$password."'";
            $result_set = mysql_query($query);
            $num=mysql_num_rows($result_set);
            if ($num > 0) 
            {
               $found_user = mysql_fetch_array($result_set);

                $_SESSION['uid'] = $found_user['id'];
                    $_SESSION['email'] = $found_user['email'];
                    $_SESSION['username'] = $found_user['username'];
                     echo "<script type='text/javascript'>
                parent.jQuery.fancybox.close();
                location.reload();
                </script>";

            }
            else
            {
            header("location:login-register.php?error=1");
            }

        }

I am running this script inside my login page. It works perfectly in xampp localhost server but session id fails to load and work when I uploaded my folder to web-server which is using Fedora 13 with php version 5.3.5.

I have checked PHPinfo, session id is active but it is not working in the server. Please help.

Cammer
  • 17
  • 3

2 Answers2

0

Add session_start() to the first line of php file.

 session_start();    
 if(isset($_POST['submit']))
    {
        $username=$_POST['username'];
        $password = md5($_POST['pwd']);
        $query="SELECT * FROM users WHERE username='".$username."' AND pwd='".$password."'";
        $result_set = mysql_query($query);
        $num=mysql_num_rows($result_set);
        if ($num > 0) 
        {
           $found_user = mysql_fetch_array($result_set);

            $_SESSION['uid'] = $found_user['id'];
                $_SESSION['email'] = $found_user['email'];
                $_SESSION['username'] = $found_user['username'];
                 echo "<script type='text/javascript'>
            parent.jQuery.fancybox.close();
            location.reload();
            </script>";

        }
        else
        {
        header("location:login-register.php?error=1");
        }
Kelvin
  • 690
  • 3
  • 11
  • thank you .. !! Is this is for web-servers or hosting server ? .. Because it doesn't show any error in local server (xampp/localhost). – Cammer Feb 23 '16 at 05:30
  • if you have not yet add `session_start()` you cannot use session in PHP both local and server http://www.w3schools.com/php/php_sessions.asp – Kelvin Feb 23 '16 at 05:33
  • Yes, I have added session_start(); in config.php file. and in every page i am calling this script "include("js/config.php");" So now what is the problem, why it is not running in webserver, but running in local server .. – Cammer Feb 23 '16 at 05:46
  • you maybe try add ob_clean() before session_start(); – Kelvin Feb 23 '16 at 05:49
  • sorry, this is also not working .. I tried to put this in individual pages and also in config file where my session_start(); is running. – Cammer Feb 23 '16 at 07:06
  • thank you, I have solved the problem. path problem is there as session folder is not created.. it's running gud now . thanks – Cammer Feb 25 '16 at 05:12
0

Please add session_start() at the top of the file in order to use session

Side note :

1) Please dont use mysql_. It is deprecated and removed from PHP 7.Use mysqli_* or PDO.

2) Your query is open for sql injection. Read this : How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • thank you .. !! Is this is for web-servers or hosting server ? .. Because it doesn't show any error in local server (xampp/localhost). – Cammer Feb 23 '16 at 05:31
  • Session is not server specific. You have to use `session_start()` at the top of the file. – Mr. Engineer Feb 23 '16 at 05:33
  • Yes, I have added session_start(); in config.php file. and in every page i am calling this script "include("js/config.php");" So now what is the problem, why it is not running in webserver, but running in local server .. – Cammer Feb 23 '16 at 05:45
  • Just add `session_set_cookie_params(0, '/', '.domainname.com');` before your `session_start()` and replace `domainname.com with your domain name – Mr. Engineer Feb 23 '16 at 05:51
  • sorry, this is also not working .. I tried to put this in individual pages and also in config file where my session_start(); is running. – Cammer Feb 23 '16 at 07:07
  • Did your provide `.YOUR_DOMAIN_NAME`? – Mr. Engineer Feb 23 '16 at 07:09
  • Yes sir, I provided our domain name. – Cammer Feb 23 '16 at 08:50
  • thank you, I have solved the problem. path problem is there as session folder is not created.. it's running gud now . thanks – Cammer Feb 25 '16 at 05:12