Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 06:00 AM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
getting post and session in a file

Hi, now that my site is populated i cant just quickly sneak a peak at the session or post like i used to with live customers on the site.

So im wanting to learn how to print_r to a file rather than to the browser, that way i can troubleshoot without interferring with members visits.

Thanks.

If i cant get this working right i will come back and let you all know whats up, but i dont want to just come here first, i want to figure it out or try like heck to figure out the issue im having with the session.
More later if i hit a dead end.

Hope i can learn how to do a file rather than a browser display so i can start on this troubleshooting, thanks.

Last edited by durangod; 10-05-2012 at 07:36 AM..
durangod is offline   Reply With Quote
Old 10-05-2012, 06:30 AM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Use the second argument to return rather than print:
PHP Code:
file_put_contents(
    
'./print_r.' date'YmdHis' ) . '.txt',
    
print_r$varTRUE )
); 
__________________
ZCE
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
durangod (10-05-2012)
Old 10-05-2012, 06:43 AM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
PHP Code:

file_put_contents
(
    
'./print_r.' date'YmdHis' ) . '.txt',
    
print_r$_SESSIONTRUE )
); 
so that would put it in print_rwhatever the date is.txt

right.. and it would write it in the same folder i am in currently or the root?
durangod is offline   Reply With Quote
Old 10-05-2012, 07:09 AM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Yup, it'd add the current date/time with no punctuation, such as print_r.20121005020936.txt, where 20121005020936 is October 5th, 2012, 2:09:36am, and place it in the same folder as the current file.

You could also just hard code the file name as `print_r.txt` or whatever you'd like and continue to overwrite it if you plan on checking in on the file each and every refresh.

Or use session_id() for a per-user basis:
PHP Code:
file_put_contents
    
'./print_r.' session_id() . '.' date'YmdHis' ) . '.txt'
    
print_r$_SESSIONTRUE 
); 
...but that would add up to a lot of files in a short amount of time if it's a popular site. On the other hand, overwriting would have you missing out on a lot of data as each hit would wipe the previous data.
__________________
ZCE

Last edited by kbluhm; 10-05-2012 at 07:18 AM..
kbluhm is offline   Reply With Quote
Old 10-05-2012, 07:32 AM   PM User | #5
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
well i thought i had it, it didnt work so i even tried something very very simple

PHP Code:
$file 'aaad_r.txt'//this should put it at the very top of the file names(easy find)
$current "hello there\n";
file_put_contents($file$current); 
i even created the file name first and ran this and the file is empty

checked every dir all the way back to the root and no file.

Last edited by durangod; 10-05-2012 at 07:34 AM..
durangod is offline   Reply With Quote
Old 10-05-2012, 07:36 AM   PM User | #6
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Nevermin i got it, i changed the permissions of the file to 666 and whallla content ...

Thanks so much
durangod is offline   Reply With Quote
Old 10-05-2012, 12:49 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by durangod View Post
i cant just quickly sneak a peak at the session or post like i used to with live customers on the site.
durangod... yes you can. As always, php can do almost anything.. including storing session data to a database instead of a file. That then means you can (in your admin panel) pull out session data and make adjustments to it / see whats in it.

Run this in phpmyadmins SQL page:
Code:
CREATE TABLE `sessions` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `session` varchar(42) NOT NULL,
 `data` text NOT NULL,
 `created` int(11) NOT NULL,
 `modified` int(10) unsigned NOT NULL DEFAULT '0',
 `closed` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=260 DEFAULT CHARSET=utf8
Now, here is sessions.class.php:
PHP Code:
<?php
class FileSessionHandler
   
{
   protected 
$savePath;
   protected 
$sessionName;

   function 
open($savePath$sessionName)
      {
      
$this->savePath $savePath;
      
$this->sessionName $sessionName;
      }

   function 
close()
      {
      
//
      
}

   function 
read($id)
      {
      
$Query "select * from sessions where `session` = '$id' and `closed` = '0'";
         
      if ((
$Result mysql_query($Query)) and (mysql_num_rows($Result)))
         {
         
$Row mysql_fetch_array($Result);
         
$_SESSION unserialize($Row['data']);
         }
      else
         {
         
$_SESSION = array();
         }
      }

   function 
write($id$data)
      {
      
$data mysql_real_escape_string(serialize($_SESSION));
      
$Time time();
     
      
$Query "update sessions set `data` = '$data', `modified` = '$Time' where `session` = '$id' and `closed` = '0'";
      
mysql_query($Query);
     
      if (!
mysql_affected_rows())
         {
         
$Time time();
         
$Query "insert into sessions (`session`, `data`, `created`) values ('$id', '$data', '$Time')";
         
mysql_query($Query);
         }
      }

   function 
destroy($id)
      {
      
$Time time();
      
$Query "update sessions set `closed` = '$Time' where `session` = '$id'";
      
mysql_query($Query);
      }

   function 
gc($maxlifetime)
      {
      
//
      
}
   }

$handler = new FileSessionHandler();
session_set_save_handler
   
(
   array(
$handler'open'),
   array(
$handler'close'),
   array(
$handler'read'),
   array(
$handler'write'),
   array(
$handler'destroy'),
   array(
$handler'gc')
   );

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');
?>
Now here is the way to handle a session in a script:
PHP Code:
include('mysql.php');
include(
'sessions.class.php'); //Note you need mysql connection for this

session_start(); 
You're a smart guy, you'll see how to modify the table to link it to your users accounts. Then once thats done you can simply use your admin pages to inspect / change the session data and put it back into the database using serialize and unserialize functions.

Of course if you really want... you can always use code to open each users session file on the disk instead... but that really is a bit more complex.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
durangod (10-05-2012)
Old 10-05-2012, 06:16 PM   PM User | #8
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Very nice tango thanks so much, above and beyond the call of duty, thanks for that. And thanks for the compliment i am proud of some of the things i have done and things i have overcome but then again i have those days where i feel like a third grader trying to figure out how to use a calculater lol...


Such as my current issue as to why if using sessions which is suppose to be a superglobal and should be available anywhere in the script. Then why when i do

PHP Code:
$_SESSION['AdminArea'] = 'online'
does it not show up when i look at $_SESSION, its driven me nuts lol... but ill figure it out eventually, hopefully before all my hair is gone lol

Last edited by durangod; 10-05-2012 at 06:23 PM..
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:37 PM.


Advertisement
Log in to turn off these ads.