PHP File read or database read?

by 10 replies
12
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?
#programming #database #file #php #read
  • 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.
    • [1] reply
    • 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.
  • 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.
    • [1] reply
    • 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.
  • 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;
    }
  • 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.
  • 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.
  • 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.
    • [1] reply
    • def go with MYSQL. Way safer than flat file systems
      • [1] reply

Next Topics on Trending Feed

  • 12

    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.