PHP File read or database read?

10 replies
Hey experts!
I have a question for you. This is what I want to do.
If someone comes to a page on my site, they are redirected to a different section depending on if their name exists in a list. I have the URL params already set up to pull the value that I need. What I am missing though is a way to have the PHP code go and read a file and see if that person's name exists on the list. If it does exist on the list, then I want them to be redirected to a different page. If their name does not exist, then I want to continue on the page. Basically, break out.

Does anyone have an idea as to how this would look in the code to read from a file and then return a boolean operator to complete this function?

Is this easier to perform in a database?
#database #file #php #read
  • Profile picture of the author freehugs
    Honestly, SQL just makes things easier. It's a little more work to get things set up initially (because you have to learn how to connect to the database and how to run queries), but this is how most programmers would solve your particular problem.

    Go to w3schools.com and read up on PHP/ MySQL. It's really not very difficult once you understand the basics.
    {{ DiscussionBoard.errors[4088278].message }}
    • Profile picture of the author chipsgear
      I am needing this semi-quickly. Have you worked with someone here in the forum that has done this work before that you could refer me to? Or maybe yourself?

      Thanks.
      {{ DiscussionBoard.errors[4088559].message }}
  • Profile picture of the author AdwordsMogul
    MySQL is the way to go. It's much faster, more secure and it will be easier to add extra functionality in future.

    Using a file poses a greater risk of someone accessing your data.
    Signature
    "Those who can - DO IT. Those who can't, say it's impossible."
    Jean Paul a.k.a AdwordsMogul
    PHPDevelopers.net - Top of the range PHP developers

    Easy Link Saver - Are you tired of the pain of constantly searching for your affiliate links? ( Chrome extension - FREE )
    {{ DiscussionBoard.errors[4089762].message }}
    • Profile picture of the author dudeontheweb
      It would be relatively easy what you want to do.

      Have your list of names on a file with each name on a new line.
      Use the file() function to read the file contents and put the data into an array.
      Then use the in_array() function to check to see if the supplied value is in the array.
      If in_array is TRUE then send visitor to the redirect page, if FALSE keep the visitor on page.

      Agreed. MySQL is the way to go, but if you need something now, then this would be a way to go. There are ways to be secure using a file db, do a little research.
      Signature

      Need a QR Code? Check out my QR Code Generator. It's FREE!

      {{ DiscussionBoard.errors[4091129].message }}
  • Profile picture of the author st8ic
    dude is right, the code for the easy option would go thusly, sire:

    function is_allowed($user)
    {
    $users = array();
    $fp = file("users.txt");
    foreach ($fp as $num => $line)
    {
    $users[] = $line;
    }

    if (in_array($user, $users)) return true;
    else return false;
    }
    {{ DiscussionBoard.errors[4091365].message }}
  • Profile picture of the author ShiftySituation
    You shouldn't go with the text file option... unless you plan on sticking with just a few names in the list as when you get a big list, it would run sluggish and you couldn't associate information with that name. In a database, you can easily make a query to "SELECT * FROM table_name WHERE name='$variable'". Then check the query with mysql_num_rows() to see if there is more than 0 rows, if so, you have them saved. Now you can pull other information from their name that you have saved in the database, like demographics or last IP they visited with...

    Now to truly answer your question, a text file is easier because you don't have to set up a database and give user privileges, etc. Just keep in mind that if you plan to scale it, you are going to have to start over again with a database.
    {{ DiscussionBoard.errors[4097291].message }}
  • Profile picture of the author ussher
    This is EXACTLY what a database is for. use php and mysql.

    If you don't find a programmer to do it here immediately, post it as a project on my site earner.net and i will get it set up for you on tuesday.

    Real simple and it will be expandable. and like adwordsmogul said much more secure.

    Use a text file and you had better secure the directory that its in so noone can access it or put it above root. Both more hassles than setting up a mysql database.
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[4097297].message }}
  • Profile picture of the author iaxia
    I have converted people off the Text option more then a few types. Go with MySQL and then when your list grows you will not be forced to do it later because of the speed difference.
    {{ DiscussionBoard.errors[4100736].message }}
    • Profile picture of the author newbie365
      def go with MYSQL. Way safer than flat file systems
      {{ DiscussionBoard.errors[4101309].message }}
      • Profile picture of the author chipsgear
        Thanks Everyone for your comments. They are GREATLY appreciated!
        {{ DiscussionBoard.errors[4103547].message }}

Trending Topics