Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Alright I am a total newb at PHP and coding, and frankly I can't wrap my head around it. But I'm having a problem:

code:
<?php
$ipLog='ipLogFile.txt'; // logfile name 
$timeout='1'; // How many hours to block IP
$goHere='banned.html'; // Allowed pages

function recordData($REMOTE_ADDR,$ipLog,$goHere)
{ 
    $log=fopen("$ipLog", "a+"); 
    fputs ($log,$REMOTE_ADDR."][".time()."\n"); 
    fclose($log); 
    
} 
function checkLog($REMOTE_ADDR,$ipLog,$timeout) 
{
    global $valid; $ip=$REMOTE_ADDR;
    $data=file("$ipLog"); $now=time();

    foreach ($data as $record) 
    {
        $subdata=explode("][",$record);
        if ($now < ($subdata[1]+3600*$timeout) && $ip == $subdata[0]) 
        {
            $valid=0; Header("Location: banned.html"); exit(0);
            break;
        }
    }
} 
checkLog($REMOTE_ADDR,$ipLog,$timeout);
if ($valid!="0") recordData($_SERVER['REMOTE_ADDR'],$ipLog,$goHere); 
?>
I want to have this script record the user's IP address and a timestamp on page load, then block access to said page until an hour later.

This correctly keeps the log, but it won't block the user out.

What am I doing wrong?

Adbot
ADBOT LOVES YOU

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Wow, that was easy. Thank you sir!

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Okay, now how would I display inline the amount of time remaining per IP?

For example, taking the ip log recorded from above and in a new page I would have:

code:
<b> You have *PHP here to show time remaining for this IP* minutes left until you can try again</b>

Treytor fucked around with this message at 05:02 on Mar 25, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

admiraldennis posted:

I'm assuming he's just trying to learn how to program or something, not implement this (I hope).

Well... I am looking to implement this. I realize a sql database would be much better, but that is WAY beyond me at the moment. One step at a time here...

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

admiraldennis posted:

You sound like you're in a little bit over your head here.

Let me guess, this is for that "prank call people from the internet" thing? I think I recognize you.

If so, you should probably consider paying somebody to do the code correctly for you instead of asking a bunch of questions here just so you can use some crappy file database that's going to choke to death if your site ever gets a lot of traffic.

Yeah, you're right. The problem is I don't have any resources to hire anyone, and I'm also fairly interested in learning a bit about this stuff. If this site proves a worthy venture, then I'll look into more appropriate options down the road.

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
I have since upgrade to PHP 5 and it seems my method of querying a server via a predefined URL no longer works:
code:
copy($url, '/dev/null');
How should this be done, now?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Yeah, to be honest I wasn't even using fopen before. Thanks!

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
I suck at PHP, I admit. All I really know how to do is modify code already given to me, and somewhat follow what is going on. Anyway, let me cut to the chase.

How do I do this:

code:
<?php
error_reporting(E_ALL);

echo "Testing....<br></br>";
$postData = array();
$postData['request_type'] = "PLAY_MSG";
$postData['destination'] = "1234567890";
$postData['file_name'] = "rr";
$postData['CID'] = "1234567890";
$postData['key'] = "1234";
$url = "http://www.server.com/handle_req.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);
if($response)
echo $response;
else
echo "Got no response";
?>
Without using curl, but fopen instead?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Thanks for the input. I was also told that something like this would work as well:
http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

But I am having a hard time wrapping my head around what to do, exactly.
Like I said, I'm only into PHP about a week here, and I'm still trying to remember where to put the semicolons :(

edit: This is what I've got: (ignore the stupid echo's I have in there)
code:
<?php
error_reporting(E_ALL);

echo "Testing....<br></br>";
$postData = array();
echo "Building postData array<br></br>";
$postData['request_type'] = "PLAY_MSG";
$postData['destination'] = "1234567890";
$postData['file_name'] = "rr";
$postData['CID'] = "";
$postData['key'] = "";
$url = "http://www.server.com/takeit.php";

echo "Compiling array<br></br>";
$postData = http_build_query($postData);

echo "opts<br></br>";
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postData
    )
);

echo "stream context create<br></br>";
$context  = stream_context_create($opts);

echo "get result<br></br>";
$result = file_get_contents('http://www.server.com/takeit.php', false, $context);

if($result)
echo $result;
else
echo "Got no response";

?>
When I run that, I get this as a response:
code:
Testing....

Building postData array

Compiling array

opts

stream context create

get result

Got no response
after about 30 seconds (timeout).

What am I missing here? The original code works perfectly on a server that supports curl, but that isn't an option...

EDIT 2: It seems my server is being a bitch...

Treytor fucked around with this message at 04:58 on Apr 1, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
This is an ip logger and checker, which outputs to a text file on the server. Is there a more efficient way to do this? Say for example if my site was getting hammered and my host was complaining about the PHP slowing down the entire server...

code:
function recordData($REMOTE_ADDR,$ipLog)
{ 
    $log=fopen("$ipLog", "a+"); 
    fputs ($log,$REMOTE_ADDR."][".time()."\n"); 
    fclose($log); 
    
} 
function checkLog($REMOTE_ADDR,$ipLog,$timeout) 
{
    global $valid; $ip=$REMOTE_ADDR;
    $data=file("$ipLog"); $now=time();

    foreach ($data as $record) 
    {
        $subdata=explode("][",$record);
        if ($now < ($subdata[1]+300*$timeout) && $ip == $subdata[0]) 
        {
            $valid=0; Header("Location: denied.php"); exit(0);
            break;
        }
    }
} 
checkLog($_SERVER['REMOTE_ADDR'],$ipLog,$timeout);
if ($valid!="0") recordData($_SERVER['REMOTE_ADDR'],$ipLog); 
EDIT: also, how would I delete expired entries?

Treytor fucked around with this message at 03:36 on Apr 2, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
This isn't on my web host, and I don't have (free) access to the crontab on the server.

I realize this would work better in a DB, but that's beyond me at this point. Time is pressing and I would think if the script just deleted any entry older than the time specified it would help keep processing cost down?

EDIT:

Here is the script edited a bit, and this is what will get run repeatedly as people refresh like mad. What line do I throw in there to check to see if a time stamp is $timeout older than $now? And if so, delete that entry.

code:
<?php
$ipLog='ip.txt'; // Your logfiles name here
$timeout='300'; // How many seconds to block IP

function checkLog($REMOTE_ADDR,$ipLog,$timeout) 
{
    global $valid; $ip=$REMOTE_ADDR;
    $data=file("$ipLog"); $now=time();

    foreach ($data as $record) 
    {
        $subdata=explode("][",$record);
        if ($now < ($subdata[1]+$timeout) && $ip == $subdata[0]) 
        {
            $stamp = ($subdata[1]+$timeout) - $now;
			$timeleft = date('i', $stamp);
	    return $timeleft;
        }
    }
	    if(!isset($timeleft))
    {
        Header('Location: index.php');

    }

}

$timeleft = checkLog($_SERVER['REMOTE_ADDR'],$ipLog,$timeout);
?>

Treytor fucked around with this message at 04:18 on Apr 2, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

duz posted:

Instead of using explode and what not, look up fgetcsv and fputcsv.

Can you chat on gmail?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Okay I've got a question. I have a form that I want filled out, but I want a hidden field to be randomized.

code:
<input name="random_file" type="hidden" value="cn1" />
I want the value to be a random selection from an array, cn1 through cn30.

Any thoughts on how this could be accomplished?

edit: I think I figured it out!

code:
<input name="random_file" type="hidden" value="<?php echo "cn".rand(1,31); ?>" />

Treytor fucked around with this message at 08:49 on May 24, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
I have a file upload script based on this - http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2293&lngWId=8

but I need it to remove spaces in the file names before it uploads it to the server. How would this be done?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

duz posted:

str_replace actually. He got a hold of me via IM and got it worked out.

Yes. Thanks again! Also thank you bt for your input as well!

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Could a PHP script be used to ping a server, and redirect to two different pages depending on whether the pinged server responds or not?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

fletcher posted:

PEAR Ping

Redirect with this

Thanks for this, but I am having problems getting PEAR to work with my web host, I think they may be blocking some functions required by PEAR to operate...

Is there any other option (preferably without requiring a framework?)

I found this script here: http://www.greenbird.info/xantus-webdevelopment/ping

Which seems to work on their site, but when I upload it and test on my own server, I'm getting a reported ping response from all servers, including the fake one...

Treytor fucked around with this message at 08:58 on Jun 22, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
How do I pause a PHP script? I've played with the sleep(); function, but it seems to 'lock' the rendering of the page until the sleep timer is up.

I want to set variables -> render a web page -> *wait 2 minutes* -> then execute code using the variables set earlier.

Is this possible?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Thanks again for your feedback, guys.

I don't really understand that fork thing, but am willing to give it a shot (I'm very new to PHP). This application is not mission critical, so worrying about server crashes is minimal.

I've also tried the flush(); thing, but that's still preventing the page from loading.

Here is what I have:

code:
<?
// php code, set vars and other super awesome stuff
flush();
?>

<HTML>
I want this to load immediately
</HTML>
<?
sleep(120);
// other PHP code, notably a form post sent out, which I want to happen 2 minutes after page load.
?>
Once I get this working, I'll share what it is I am doing exactly.

EDIT: I don't have terminal access or to a cron job, as this is fairly basic web hosting. A cron wouldn't be frequest enough, anyway as I want this to run exactly 2 minutes after page load. With a cron, there could be a minute delay if they page load right after the last cron goes through.

Treytor fucked around with this message at 00:27 on Jul 19, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

jasonbar posted:

No sir, flush AFTER the html, right BEFORE the sleep(). Here is a working example: http://burntmail.net/flush.php

Would PHP being in safe mode interfere with this at all? Your script works perfectly on your page (obviously), but if I copy that code exactly and run it on mine, the page doesn't load until after sleep is done.

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!

jasonbar posted:


php:
<?php
// your html
ob_end_flush(); // note that you need not call ob_start()
flush();
sleep(5);
?>


This is working perfectly, thank you!

Now to answer your question as to what I am doing, exactly:

http://www.ecarddialer.com

Thanks again!

Treytor fucked around with this message at 11:55 on Jul 23, 2008

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Is there any way to pass a variable from one page to another within PHP without it being visible to the end user or using a database?

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Is there an easy way to mask the source of a download link? For example, I have an file at http://192.168.0.1/soundfile.mp3, but I want it to appear to be coming from http://www.example.com/soundfile.mp3

Can this be done easily?

Thanks

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Yes you are correct on your assumptions. I guess I just have no idea how to do what you're saying, but it's promising to hear that it is possible.

Adbot
ADBOT LOVES YOU

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Thanks again for the help, I really appreciate it. The two servers in question here are not on the same network, but are both web accessible. I don't want my end user to be aware of the IP address server, only the domain name.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply