SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting data from session file

    Hi guys,

    Im a little stuck here and was hoping for some help if possible??

    I am trying to extract a product code from a sessions file for use in a MYSQL database query.

    This is sample line (entry) from the sessions file:
    Code:
    ABC123 item name 129.95 1 N;
    How can i just extract the item number 'ABC123' so I can use it in the query ($inv_query) below?

    PHP Code:
    $session_id $_SESSION["sessionid"];

    $fp fopen ("./sessions/".$session_id.".dat""r");
    while (
    $data fgetcsv ($fp500)) {

    //setup values from array
        
    $item_id $data[0]; //we'll test for this one below to ensure we don't bring in blank rows
        
    $item_qty $data[3];

    $inv_query "SELECT * FROM Products_Test WHERE product_id='$item_id'"
    Any help would be greatly appreciated.

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adammc View Post
    Hi guys,

    Im a little stuck here and was hoping for some help if possible??

    I am trying to extract a product code from a sessions file for use in a MYSQL database query.

    This is sample line (entry) from the sessions file:
    Code:
    ABC123 item name 129.95 1 N;
    How can i just extract the item number 'ABC123' so I can use it in the query ($inv_query) below?

    PHP Code:
    $session_id $_SESSION["sessionid"];

    $fp fopen ("./sessions/".$session_id.".dat""r");
    while (
    $data fgetcsv ($fp500)) {

    //setup values from array
        
    $item_id $data[0]; //we'll test for this one below to ensure we don't bring in blank rows
        
    $item_qty $data[3];

    $inv_query "SELECT * FROM Products_Test WHERE product_id='$item_id'"
    Any help would be greatly appreciated.
    I think you're close to what you want. I didn't do any testing, but try changing this line:

    PHP Code:
    while ($data fgetcsv ($fp500)) { 
    to

    PHP Code:
    while ($data fgetcsv ($fp500' ')) { 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    SitePoint Zealot stuffedbuggy's Avatar
    Join Date
    Sep 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you know that the exact length of the string you're wanting to retrieve within a string, just use...

    Code:
    $stringtosearch ='ABC123 item name 129.95 1 N';
    $stringtofind = substr($stringtosearch,0,6);
    echo $stringtofind;
    In $stringtofind the '0' refers to the starting point of the string, and the '6' refers to how many characters to search for..
    You know you cooler than me...

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There should be absolutely no need to use fread() or its filesystem equivalents to access data in a session file. That's the purpose of $_SESSION

    To extract a specific string from that session data, either restructure the way you hold your session data, or the code that stuffedbuggy has posted
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also let php just load the data into $_SESSION for you.

    I assume $session_id contains a different session id than the current one being used to associate with the data in _SESSION?
    PHP Code:
    // close/save current session
    session_write_close();
    // temporarily tell php not to send sess cookies
    ini_set('session.use_cookies'false);
    // tell php which file to load data from
    session_id($session_id);
    // load it
    session_start();
    /*
    ...do what you may with it...
    when you're done, you can close this session like we just did and reload the other one if needed.
    */ 

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If the session file will always have the first element as the code then you can search in this string the first occurrence of space using stripos(). This will return you the position and now you can fetch a part of the string from the beginning till the position returned. You can use strstr() to get the sub string.

    Hope this makes sense to you.
    Last edited by r937; Dec 20, 2008 at 04:17.
    Maneet Puri
    Lexolution IT Services

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You could also let php just load the data into $_SESSION for you.

    I assume $session_id contains a different session id than the current one being used to associate with the data in _SESSION?
    PHP Code:
    // close/save current session
    session_write_close();
    // temporarily tell php not to send sess cookies
    ini_set('session.use_cookies'false);
    // tell php which file to load data from
    session_id($session_id);
    // load it
    session_start();
    /*
    ...do what you may with it...
    when you're done, you can close this session like we just did and reload the other one if needed.
    */ 
    Yes, I think this is the right way to work with sessions rather than reading the file directly with PHP file sytem functions and extract, etc. I don't know why you (OP) are compelled to do so to retrieve the sessions values by reading the file.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •