1

I have some simple system to upload files and keep track of them for each particular user, using a database.

The problem of mine is, I connect to the database in the file checklogin.php, which is responsible to handle the $_POST from main_login.php.

In file 'checklogin.php':

$current_user_name = NULL;

which is a global variable for all files. Now in file signup.php, I try to include the checklogin.php to define that variable:

require_once '/checklogin.php';
...
mysql_query("INSERT INTO " . tbl_name . " (username, userpassword, userisadmin) 
         VALUES (''" . $_POST['myusername'] . "',"
         . "'" . md5($_POST['mypassword']). "',"
         . "0)");
$current_user_name = $_POST['myusername'];
header("location:login_success.php");

As you can see, I'm trying to set the value of the variable $current_user_name = $_POST['myusername'];, but when header goes to the file login_success.php, which is having require_once '/checklogin.php'; too, the variable is set again to null.

How can I solve this problem? i.e. How can I store the current user so that it is accessible by all files?

hakre
  • 193,403
  • 52
  • 435
  • 836

3 Answers3

2

You cannot store a variable like that. Each request will be new execution in sever. In this kind situation we have to use session please check this

And another issue with your code is SQL injection, Please read this too

Sanoob
  • 2,466
  • 4
  • 30
  • 38
1

You can not access the Parameter received at checklogin.php

what you can do you can check the the login status and set the current user in session.

From session variable you can access and set the current user.

1

you can set a session variable for it and on every you can use it like this

session_start();
    if(isset($_SESSION['current_user_name']))
    {
      $current_user_name = $_SESSION['current_user_name'];
    }
    else
    {
      $current_user_name = NULL;
    }

and set your session variable as follows

session_start();
require_once '/checklogin.php';
////...
mysql_query("INSERT INTO " . tbl_name . " (username, userpassword, userisadmin) 
         VALUES (''" . $_POST['myusername'] . "',"
         . "'" . md5($_POST['mypassword']). "',"
         . "0)");
$current_user_name = $_POST['myusername'];
$_SESSION['current_user_name'] = $current_user_name; // set your session here
header("location:login_success.php");
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51