Jump to content

EsOne

Members
  • Posts

    59
  • Joined

  • Last visited

    Never

Posts posted by EsOne

  1. I'm actually new to PHP too. I can't really help much in any way, but don't let something be too hard for you. I would pick one specific thing at a time, and incorporate that and get used to doing that one thing first. As japesu said, something easy. As for tutorials, if you learn better by video, the money for a subscription to lynda.com is great. They have quite a few tutorials on PHP MYSQL. That's what I learned from. I started learning PHP a few days ago. A week at most, and I have already set up my first basic page that scrapes information from a remote source, reads it, uses data from it, and echos a string based on what information it finds. I also learned how to set up a script that will log IPs. Just really basic stuff, but it was very practical for what I was wanting to do at the time. The tracking IPs lesson I did (was actually right here in these forums a few hours ago) was the first time I made and used a database. Sure, the formatting looks sloppy, but it gives you a base.

     

    tl;dr

     

    Stick with it, and be excited about the new stuff you learn. Just pace yourself. Good luck!

  2. I did that by myself too! That makes me happy! LOL. You don't understand how happy I am getting. I think I am getting on fiances nerves because I keep YAYing out loud. LOL.

     

    I did it by:

    $IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
    $Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
    $Date =date('D M dS Y h:i a'); //The date, you can use other info..
    $agent = getenv("HTTP_USER_AGENT"); //Or whatever refined IP //Used the getenv code to collect the USER AGENT and assign it to $agent
    
    
    
    
    mysql_query("INSERT INTO ip_table (IP, requests, date, time, agents) VALUES('".$IP."','".$Requests."', '".$Date."', '".$Time."', '".$agent."' ) ") or die(mysql_error()); // Added agents to the INSERT section, so it knows to insert the value of $agents (at the end of VALUES) into it.

     

    I understand the principle, but what exactly does "getenv" mean?

  3. Die does as you'd expect it, It kills the output buffer and prevents any further code being run, Displaying only the message. preg_match has two parameters, pattern and match, so it's just matching if the phrase 'MSIE' exists in '$agent'.

     

    And yeah, You can add as many browsers as you want (you can do an array if you're willing to instead of if's, just an example code), and the /i means insensitive, so it can be 'SaFarI' and match "Safari" or "safari".. I use this on a script of mine, just pulled out a bit of it.

    Would I be able to create a table in MYSQL that stores the whole USER_AGENT text, then echo that out so it comes in the format you mentioned before (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 )? Kind of like Wordpresses tracker.
  4. This HTTP_USER_AGENT you referred to. What exactly does it do, and where would it be used?

     

    It's the user agent of the browser (Note it is sent by the client, and remains as an environment variable on the server http://php.net/manual/en/function.getenv.php ) for the site to be able to know the browser's type (I.E./Firefox/Safari etc.) and would come out as something such as:

    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 

     

    Note most bots do not , or do not care to define a user agent, And most e-mail harvesting bots are named. You may want to match it against all the common browsers to further sanitize your pages..

     

    $agent = getenv("HTTP_USER_AGENT");
    if (preg_match("/MSIE/i", $agent)) {
    } elseif(preg_match("/FIREFOX/i", $agent)) {
    } elseif(preg_match("/MOZILLA/i", $agent)) {
    } else {
            die('Uh oh, Bot?')
    }

     

    Bleh, messy example but easy to do, preg_match isn't as hard as it looks,  you may want to add Chrome and a few others as well if you wish to use this method, Or you can simply just check if it's empty, an easier way.

     

    So broken down it would be:

     

    $agent = getenv("HTTP_USER_AGENT"); //setting the agent into the variable $agent
    if (preg_match("/MSIE/i", $agent)) {  // If the term MSIE is in there, dump it to $agent (?)
    } elseif(preg_match("/FIREFOX/i", $agent)) {  //Same as above just with FF
    } elseif(preg_match("/MOZILLA/i", $agent)) { /* Would I be able to add Safari by doing "/Safari/i"? Just wondering. I don't want to block someone because they are using a weird browser. */
    } else {
            die('Uh oh, Bot?') // Does this die command stop it from producing the rest of the PHP on the //screen?
    }

  5. I figured it out! YAY! That makes me happy that I was able to debug it by myself!

     

    All I did was change it to $Date =date('D dS M,Y h:i a');, and it shows as:

    xx.xxx.xxx.xx Sat Dec 12th,2009 05:26 pm

     

    Oh, and checking the user agent via getenv("HTTP_USER_AGENT") can aid you in removing bots, and if there is no user agent, than it is either a bot or untrusted. I forgot to think of this.. This should help you much more than IP filtering, it's vague.

     

    May be a fun tool to put in your database as well as the others, since you can compare IPs and what browser they're using, much easier to sift them out and add them to a 'blacklist' (with preg_replace or whatnot) if you need.

     

    This HTTP_USER_AGENT you referred to. What exactly does it do, and where would it be used?

  6. I've gotten quite a bit of help here, so I figured I would get around to introducing myself since I plan on being around for a while.

     

    I am a newbie (and do I mean NEWBIE!) at PHP, or any coding in general.

    I technically have just started learning PHP a few days ago, maybe a week max. So far I have managed to do some simple scraping, and IP tracking and recording scripts for my webpage. Just simple stuff, but hey, it excited me to know I created it.

     

    I will be attending college next month, majoring in web design. However, because of prequsites, I will be unable to take PHP classes until spring. So, I will be taking intro to HTML/xHTML this first semester.

     

    I am glad I found a place to ask questions without being outright ignored, or looked down on for my lack of knowledge. I have a subscription to Lynda.com that has helped me even get to where I am now. But, I find these forums a little better when I have really specific questions. And, my subscription ends in a couple days anyway xD

     

    Well, as usual, I am talking to much. So, HI everyone!

  7. Got that to work properly.

     

    Now, I need to add Time to the equation. (Time of visit)

     

    I edited the following code with the time:

     

    $dbselect= mysql_select_db("greg1233",$connection);
    
    $IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
    $Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
    $Date =date('Y-m-d'); //The date, you can use other info..
    $Time =time('h.m.s');
    
    mysql_query("INSERT INTO ip_table (IP, requests, date, time) VALUES('".$IP."','".$Requests."', '".$Date."', '".$Time."' ) ") or die(mysql_error());
    

     

    It does register something in the MYSQL, but it is WAY inaccurate, showing the time as 839:59:59

  8. AWESOME! after I changed the variable to $habo, and changed assoc to array again, it worked.

     

    Now. This may be a dumb question. When it displays, it comes in a single row. How do I make them show horizontally?

     

    I.E

     

    xx.xxx.xxx.xx *date*

    xx.xxx.xxx.xx *date*

    xx.xxx.xxx.xx *date*

    xx.xxx.xxx.xx *date*

    xx.xxx.xxx.xx *date*

     

     

    Also, previously, I was recommended to make a "Requests" section in my table. What does the information in here hold?

  9. It could be because you already have a field name $results or $row, but without seeing the full code its hard to say. in all reality if it worked like that it should be working the other way too. OOP is present in php as well. You can make classes and create properties, methods and variables for those classes (or object). If you are ever going to do any programming in the business world you are going to need to know OOP pretty well. Here is an example of OOP in php:

     

    class person{
      public $name;
    }
    
    $aperson = new person();
    $aperson->name = 'nick';
    
    echo $aperson->name;
    

     

    that is just a class that has a variable $name. then i create the class with a variable $aperson. Then i assign the variable $name in the class to nick then i just echo it to the screen. So OOP is everywhere.

     

    I checked for pre-exhisting $row variables. None present, but just to make sure, I changed the $row variable to $habo, since there is no way I already have a variable named habo. Same results.

     

    Here is the complete code of the page.

     

    It includes the code to ban certain IPs from banned.txt. The code I am working now for having it display results is at the bottom.

     

    <?php
    
    $connection= mysql_connect("******","******","******");
    
    if(!$connection)
    {
    die("Error" . mysql_error());
    }
    
    $dbselect= mysql_select_db("greg1233",$connection);
    
    $IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
    $Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
    $Date =date('Y-m-d'); //The date, you can use other info..
    
    mysql_query("INSERT INTO ip_table (IP, requests, date) VALUES('".$IP."','".$Requests."', '".$Date."' ) ") or die(mysql_error());
    
    
    
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    elseif (isset($_SERVER['HTTP_VIA'])) {
        $ip = $_SERVER['HTTP_VIA'];
    }
    elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    else {
        $ip = "Banned";
    }
    $banned = file("banned.txt", "r+");
    $nbanned = count($banned);
    function ban($ip, $banned, $nbanned){
        for ($i = 0 ; $i < $nbanned ; $i++) {
            
            // Use this if you want to use IP patterns with regular expressions:
            // if (eregi($ip, $banned[$i])) {
            // We have to strip the end-of-line characters, to test for equality: 
            if ($ip ==  rtrim($banned[$i])) {
                echo "You have been banned from this site, if you feel this is in error ";
                echo "please send email to you@yoursite.com ";
                die();
            }
        }
    }
    ban($ip, $banned, $nbanned);
    
    
    
    $query=mysql_query("SELECT * FROM ip_table ORDER BY ID DESC");
    while ($habo = mysql_fetch_assoc($query)) {
        echo $habo[0];
        echo $habo[1];
        echo $habo[2];
    }
    
    ?>

  10. Okay. I understand. The while loop will keep going through $query to continue to get results.

     

    Any idea why it is working without a while loop, but not with one?

     

    And BTW guys. I really do appreciate all the time you have spent helping me. If I didn't have this kind of help, I would probably be less likely to continue to learn.

     

    Lynda.com has helped me get started with the basics though. It wasn't cheap to learn. Next month I will be starting web development classes in my college, but PHP will not be explored until my second semester. I am 24 years old and feel like I should be learning something like this. Web development has always been something I have wanted to learn. I also am going to dabble in VB.NET in college as well, so I can have a small knowledge of OOP.

  11. $query=mysql_query("SELECT * FROM ip_table ORDER BY ID DESC");
    $row=mysql_fetch_array($query);
    echo($row[1]);

     

    Maybe try this..

     

    Resource ID#3 is an internal pointer that MySQL uses in the result. mysql_fetch_* should not return that.. Hmm.

     

    That worked! Well, sorda! It did show me an ip, and I added it to echo $row[3] so it shows the date, however, isn't it supposed to echo all the IPs in that row ordered by decending ID? It only showed me one IP and date.

     

    Also. What changed in that code? The only thing I really notice is the $result changed to $query.

     

  12. Nothing comes up now. No errors, but nothing else either.

     

    I echoed $result again, and it still says resource id#3

     

     

    myphpadmin2.png

     

    Just posting this again to maybe find out if I am coding it wrong for the location I am trying to get an array from.

     

    "SELECT * FROM ip_table ORDER BY ID DESC"

    I am telling it to select all data from the table ip_table and to order it decending.

     

    I think that part may be what is wrong. However, I tried "SELECT * FROM ip_table WHERE 'id' ORDER DESC" with the same results.

     

    In my table, the IP should be in row[1], and the date in row[3].

     

  13. Okay, I fixed the line 53 error. Now I am having an issue turning the data into an array. When I used the fetch_array thing, it did not return an error, but did nothing. I did some debugging, I echoed $result before the fetch array, and it came back as:

    Resource id #3

     

    After I var_dumped @result. I got this return:

     

    resource(3) of type (mysql result)

     

    And i also echoed $result AFTER I had assigned the array to it, and it still echoes as Resource id#3.

     

    Here is the updated code I am using:

     

    $result = mysql_query("SELECT * FROM ip_table ORDER BY ID DESC");
    
    echo $result . "</br>";
    
    while ($row = mysql_fetch_array($result));
    {
    echo $row["IP"]. " " .$row["Date"]."</br>";
    }
    
    var_dump($result);
    echo "</br>" . $result;

     

     

     

     

     

     

     

    @oni - I just tried that with these results:

     

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/e/s/o/esone/html/test/jsontest.php on line 55

     

    bool(false)

     

     

  14. Also, tried this to show the info on a new page:

     

    
    $result= mysql_query("SELECT * FROM greg1233");
    
    while ($row = mysql_fetch_array($result));
    {
    echo $row[1]. " " .$row[3]."</br>";
    )

     

    (while testing, this is all going in one file. Kind of like a big test compilation.)

     

    It comes back with this error:

     

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/e/s/o/esone/html/test/jsontest.php on line 53

     

    Line 53 being:

    while ($row = mysql_fetch_array($result));

  15. I changed the IP field to "text", and date to "Date", and kept the Request as "Text"

     

    While browsing the table I see my IP and the correct date, and in the Requests section I see:

     

    Array
    (
       [wp-settings-1] => m0=o&m1=o&m2=c&m3=c&m4=o&m5=o&m6=o&m7=o&mate=c&m9=c&uploader=1&hidetb=1&editor=tinymce&align=right&m10=c
       [wp-settings-time-1] => 1259685694
       [phpbb3_n8av8_k] =>
       [style_cookie] => null
       [phpbb3_n8av8_u] => 2
       [phpbb3_n8av8_sid] => 02150c018d96853be6e39d2ac537223c

     

    What exactly does this "Requests" section do? And what info does it hold?

  16. Wonderful. It worked. Now...

     

    In my above pic of my table. Do I have the types right for each section? (IP being int, Requests being text, and date being an int)

     

    I did notice some information being written to it after I visited the page. I just want the information to be in the right format for being pulled up later.

     

  17. The page you recommended goes to a 404.

     

    Oni-kun recommended

    $IP = $_SERVER['REMOTE_ADDR']; //Or whatever refined IP
    $Requests = mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
    $Date = date('Y-m-d'); //The date, you can use other info..
    
    mysql_query("INSERT INTO ip_table (IP, requests, date) VALUES('".$IP."','".$Requests."', '".$Date."' ) ") or die(mysql_error());

     

    I get the

    $IP = $_SERVER['REMOTE_ADDR']; //Or whatever refined IP

    line.

     

    It is assigning the results from the $_SERVER['REMOTE_ADDR'] to the variable $IP.

     

     

    $Requests = mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars

    Not sure on this line. The mysql_real_escape_string(print_r($_REQUEST,true)); is being assigned to $Requests, but what is it, and what exactly is the string it is assigning to $Requests?

     

    $Date = date('Y-m-d'); //The date, you can use other info..

    A bit obvious. It is assigning the date into the variable $Date. However, where is it pulling the date from?

     

    mysql_query("INSERT INTO ip_table (IP, requests, date) VALUES('".$IP."','".$Requests."', '".$Date."' ) ") or die(mysql_error());

     

    This is writing the information into MYSQL, correct? The ("INSERT INTO ip_table (IP, requests, date) VALUES('".$IP.",".$Requests."','".$Date."') ")

    is saying insert the values of $IP, $Requests, and $Data into ip_table in the IP, requests, and date sections.

     

    I have tried this, with the following:

     

    <?php
    
    $connection= mysql_connect("host","username","password.");
    
    if(!$connection)
    {
    die("Error" . mysql_error());
    }
    
    $dbselect= mysql_select_db("ip_table",$connection);
    
    $IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
    $Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
    $Date =date('Y-m-d'); //The date, you can use other info..
    
    mysql_query($dbselect,"INSERT INTO ip_table (IP, requests, date) VALUES('".$IP."','".$Requests."', '".$Date."' ) ") or die(mysql_error());

     

    I get this error:

     

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/e/s/o/esone/html/test/jsontest.php on line 16

    Access denied for user 'username'@'%' to database 'ip_table'

     

    Maybe because of how I have my MYSQL table set up?

     

    myphpadmin.png

  18. @ngreenwood - not sure I understand what you mean. Or, vice-versa.

     

    This site will be closely monitored. I will pesonally monitor who I know is on there. If I see an unexplained IP, I will try to limit it. The part of the site I am trying to do this on will only have a max of 10 users, all of which are friends of mine. If I notice an IP that differs from the 10 peoples IP, I will limit the access. Even if the persons IP is dynamic, I will still know it is them by asking them. LOL.

     

    I think I will give up on this for now. I really need to learn more about MYSQL first. Since, I don't really know how to get PHP to store the correct info in the correct place.

     

    Thanks everyone. I will be back if I need any more assistance.

  19. I have started a tutorial on MYSQL a few days ago. I know how to create tables and whatnot now.

    I just don't know how to get information from MYSQL into a php page in a usable way, or how to make a code write to the table.

     

    So, I would use the $_SERVER['REMOTE_ADDR'] to get the IP. What would the syntax look like if I wanted to put it into MYSQL?

     

    Also, to view the IPs generated easily, would it be a good idea to create another page to list the IPs that have visited?

     

    I'm not really worried about proxy use, as the site I am trying to do is but a small section. I want to limit access, but avoid "user passwords"

    I want to avoid that because the few people that would have access to it could easily give out their password to friends. Then, I would be at square one over again. Having access to limit the IPs that visit seem better. Since, even if I did decide to go with a password/username system, I could still limit access from people that should not have access.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.