0

I am creating a access log in PHP where I am storing the logged-in username , userip and userlogged-in time inside my sql server database

I want to add one more field filename into that, I am taking everything from previous pages using sessions but however, I am unable to figure out how do I take filename from the previous page

I am sharing my code too. Any help would be highly appreciated.

my code for creating access log in php and storing it inside sql-server database is

 session_start();
 include 'database_connection.php'
 $uname = $_SESSION["user"] ;
 echo $uname;
 
 function getUserIP()
 {
     // Get real visitor IP behind CloudFlare network
     if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
               $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
               $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
     }
     $client  = @$_SERVER['HTTP_CLIENT_IP'];
     $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
     $remote  = $_SERVER['REMOTE_ADDR'];
 
     if(filter_var($client, FILTER_VALIDATE_IP))
     {
         $ip = $client;
     }
     elseif(filter_var($forward, FILTER_VALIDATE_IP))
     {
         $ip = $forward;
     }
     else
     {
         $ip = $remote;
     }
 
     return $ip;
 }
 $user_ip = getUserIP();
 
 echo $user_ip; // Output IP address [Ex: 177.87.193.134]
 
 $currentDate = date('Y-m-d g:i:s');
 
 echo $currentDate;
 
 
 $sql = "insert into ACCESS_LOGOFUSERS(ALU_USERNAME,ALU_IP,ALU_CREATEDON)
 VALUES('$uname','$user_ip','$currentDate')";
 
 $res = sqlsrv_query($conn,$sql);
 
 session_destroy();
 
  ?>
 
 

my file name code which is stored on the previous page is

 $fileName = basename($_FILES["fileToUpload"]["name"]); 
 

I am unable to figure out how shall I take the $filename to another page with the help of the session so that I can also add the below code to my existing code

  $query=sqlsrv_query("insert into user_action(file_name)values('$file_name')");
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Why, where is the problem? Stick the value into a session variable, `$_SESSION['fileToUpload'] = basename($_FILES["fileToUpload"]["name"]);` or something like that, and then access it via that in the following script. – CBroe Oct 20 '22 at 06:33
  • @CBroe $FileName = $_SESSION['fileToUpload'] = basename($_FILES["fileToUpload"]["name"]); echo $FileName; I have used this and I am getting undefined error session is started on every page – Veronica Dsouza Oct 20 '22 at 07:04
  • What error (_exactly_), and referring to what line? It is hard to tell what the issue is, when you show single lines of code out of context. – CBroe Oct 20 '22 at 07:07
  • @CBroe This line is only causing error $FileName = $_SESSION['fileToUpload'] = basename($_FILES["fileToUpload"]["name"]); even-though session is started on every page – Veronica Dsouza Oct 20 '22 at 08:34
  • `$FileName = $_SESSION['fileToUpload'] =` are assignments, not _read_ access. So any error saying something was undefined, could only come from the `$_FILES["fileToUpload"]["name"]` part here. – CBroe Oct 20 '22 at 09:00
  • @CBroe How to $_SESSION WITH $_FILES – Veronica Dsouza Oct 20 '22 at 09:12
  • Storing a value into the session always works the same way, no matter where that value came from. If the error message is relating to `$_FILES["fileToUpload"]["name"]`(I asked you to tell us the _exact_ error), then you need to figure out why that is not set at the point where you are trying to do this. – CBroe Oct 20 '22 at 09:15
  • @CBroe whenever i am using $fileName = $_Session(basename($_FILES["fileToUpload"]["name"])) ; I am getting syntax error – Veronica Dsouza Oct 20 '22 at 09:30
  • How many more times do I need to say, please give us the _exact_ error. (And that line in itself does _not_ contain a syntax error, so it probably must be something before that then.) – CBroe Oct 20 '22 at 09:33
  • @CBroe i am getting this error Fatal error: Uncaught Error: Array callback must have exactly two elements on this line $fileName = $_SESSION(basename($_FILES["fileToUpload"]["name"])); – Veronica Dsouza Oct 20 '22 at 09:39
  • $_SESSION is an array, but you are trying to call a function here. Array access uses _square_ brackets. – CBroe Oct 20 '22 at 09:46
  • But even with square brackets, using `basename($_FILES["fileToUpload"]["name"])` as the array key, makes little sense. You won't _know_ what that value actually is, on your second page - so how could you _use_ that value then, to access the session entry? – CBroe Oct 20 '22 at 09:48
  • @CBroe even after using square bracket i am getting same error – Veronica Dsouza Oct 20 '22 at 09:50
  • Show the modified code, otherwise there is no way of telling what you might have misunderstood this time. – CBroe Oct 20 '22 at 09:54
  • @CBroe This time i have used this code $fileName = $_SESSION[basename($_FILES["fileToUpload"]["name"])]; – Veronica Dsouza Oct 20 '22 at 09:55
  • I don't see how that could possibly get you the same error. And as I already said, using that value as the session _key_ here, does not make much sense to begin with. – CBroe Oct 20 '22 at 09:58
  • @CBroe Uncaught Error: Array callback must have exactly two elements in i am getting this error – Veronica Dsouza Oct 20 '22 at 10:04
  • The error makes sense, as long as you used the wrong type of braces - see https://stackoverflow.com/q/71450130/1427878 But you say you fixed that now, so it makes no sense that you would still get that same error (at least not from the same place in the code) - and https://3v4l.org/tSVdl also shows nothing of the sort. (It shows a couple of other errors because the data is not set, but if the code you have shown actually caused a fatal error, that should happen in there as well.) – CBroe Oct 20 '22 at 10:11
  • @CBroe I am not getting where exactly I am getting the error – Veronica Dsouza Oct 20 '22 at 10:23
  • Why not, the error message should contain file name and line number? – CBroe Oct 20 '22 at 10:26
  • @CBroe Is this code correct ? $fileName = $_SESSION[basename($_FILES["fileToUpload"]["name"])]; – Veronica Dsouza Oct 20 '22 at 10:47
  • Syntactically, on its own - yes. But does it make sense ... No. And I already said why. – CBroe Oct 20 '22 at 10:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248929/discussion-between-veronica-dsouza-and-cbroe). – Veronica Dsouza Oct 20 '22 at 11:04

0 Answers0