www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 07-01-2009, 12:06 PM
    Peuplarchie's Avatar
    Peuplarchie Peuplarchie is offline
    Registered User
     
    Join Date: Feb 2006
    Posts: 344
    Post Match $var in text file ?

    Good day to you all,
    stupid as it sound, all the thing that I have tried to match a variable into a text file and execute according to it result.


    PHP Code:

    $active
    = "user_list.txt";
    $pos = strpos($active, $_SESSION['username']);
    if (
    $pos === false) {
    echo
    "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
    } else {
    echo
    "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
    }
    Thanks !
    __________________
    That's why we are not alone on earth, to help each other !
    Let's build !
    Reply With Quote
      #2  
    Old 07-01-2009, 01:16 PM
    SrWebDeveloper's Avatar
    SrWebDeveloper SrWebDeveloper is offline
    Registered User
     
    Join Date: Jul 2009
    Location: Falls Church, Va.
    Posts: 691
    Talking

    If I understand you correctly, you have a text file on the server named user_list.txt and wish to see if the session user name is in that file and then display the desired image based on the comparison.

    Your code as posted uses the value you set for $active which is "user_list.txt", not the file. You have no code to import the file into that variable.

    If my assumptions are correct, try this revised code:

    PHP Code:
    $active="/physical_path_to/user_list.txt";  // adjust path
    if (file_exists($active)) {
      
    $haystack=file_get_contents($active);
      
    $findme=preg_quote($_SESSION['username'], "/");
      if (!
    preg_match("/".$findme."/i",$haystack)) {
    echo
    "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
    } else {
    echo
    "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
    }
    }
    If user_list.txt is in or within your document root directory (the base folder you uploaded your web files) replace line 1 in my example code with:

    $active=$_SERVER['DOCUMENT_ROOT']."/path_to/user_list.txt";

    And adjust the path as necessary, so if the script is ever moved to a different server with a different document root path it won't break.

    The rest that follows are "btw, I noticed..." comments:

    Also, I'm sure you have your reasons for enclosing each image in a bold tag, but without text it seems odd. If you don't need the <b> tag remove it and move the ID to the img tags. Also, it's perfectly legal to use single quotes instead of double quotes in simple HTML like this. Each byte counts!

    Cheers.

    -jim
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes 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 Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:11 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.