Log in

View Full Version : General Knowledge



bluewalrus
01-30-2009, 06:51 AM
These are just some random question's I have right now kinda relating to other posts I've made and kind of not....

Is there a way to differentiation between refreshes and actually first visits possibly with a header request?

Is the date function accessing my server's date, the computer I edited originally from on, or the user accessing the sites computers date settings?

Will accessing a user's ip address with $_SERVER['REMOTE_ADDR'] ever come across to an anti-spam or virus program as a malicious site or present some question to the user?

Not really relating to PHP but how does bandwidth work? I think I understand the basics of it. Like if you have 1000 users accessing 1byte of data at the same time you need 1+kbs of space free to have that function for all users. But the in a month feature I don't see how that works, unless they are just charging you by the chance that all your users hit you at the same time? Is that right or does it back up somewhere and the bandwidth can still be hit later by some of those same time users?

Thanks for any explanation you can offer on these.

Twey
01-30-2009, 01:51 PM
Is there a way to differentiation between refreshes and actually first visits possibly with a header request?Not specifically, but you can set a cookie or session variable.


Is the date function accessing my server's date, the computer I edited originally from on, or the user accessing the sites computers date settings?Of course the server's. PHP runs on the server; it doesn't have (direct) access to anything else.


Will accessing a user's ip address with $_SERVER['REMOTE_ADDR'] ever come across to an anti-spam or virus program as a malicious site or present some question to the user?No. PHP runs on the server; client applications never see it. The client's IP address is given out in every network transaction, or the server wouldn't know where to send the requested data.


Not really relating to PHP but how does bandwidth work? I think I understand the basics of it. Like if you have 1000 users accessing 1byte of data at the same time you need 1+kbs of space free to have that function for all users. But the in a month feature I don't see how that works, unless they are just charging you by the chance that all your users hit you at the same time? Is that right or does it back up somewhere and the bandwidth can still be hit later by some of those same time users?Bandwidth is not related to speed. It's the amount of data your users can download. If you have a 200GB/month bandwidth limit, your server can only serve a total of 200GB per month before being cut off.

bluewalrus
01-30-2009, 10:43 PM
OO thanks.

About the bandwidth though so if you have your own server the limitations are just at the one time?

Twey
01-30-2009, 11:39 PM
Well, it's likely that your host or ISP will impose a limit of some sort.

bluewalrus
02-01-2009, 06:07 AM
ooo hah never thought of my isp limiting me but that does make sense now that i think of it. Thanks again.

I got another question... I don't know if it needs to be here or just in "other" cause it might need to be a math equation. Is there an IsEven() function in php. I've seen it googling but it looks like it is more of a made up function for each I found, and that the user is not getting a true/false (0,1) but rather a 0,2,4,6,8 value and then determining it from there. Although that would work it seems like a lot more work to me if there is an equation or a function that could do the same trick. Like (x/2)= 0, or any decimal, which would be odd and the rest would be 1 and even.

Thanks for any ideas you may be able to offer on this as well and for your other help as well.

JasonDFR
02-01-2009, 08:56 AM
php doesn't have a built in "is_even()" function, but you can make your own:



/**
* bool is_even ( integer )
*
* @param $int The integer value being evaluated
* @return Returns true if $int is even. Returns false if $int is odd
*
*/

function is_even($int) {

if ( !is_int($int) ) {

trigger_error('The is_even() function only accepts integers.', E_USER_WARNING);
return;

// Or get rid of trigger_error(xxx) and replace it with: return false;
// to accept any input. This will cause odd numbers and decimals
// to return false.

}

$result = $int % 2 == 0 ? true : false;

return $result;

}


I don't know how well you know php, but when I first started learning php I would see a function that someone wrote for me and not really understand it, so I'll explain mine.

is_even() as I wrote it, accepts an integer value only. The first part of the function checks to see if the input value is an integer, if not it will cause an error message to be displayed. If you want to change the type of error that is displayed, you can change E_USER_WARNING to one of the other E_USER constants http://www.php.net/manual/en/errorfunc.constants.php .

If the input is an integer, the next part of the function is executed. I used the ternary operator:

http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

The value assigned to $result will depend on whether or not $int % 2 == 0 evaluates to true or false. If true, the first value after the ? will be assigned to $result, if false, the second value will be assigned.

$int % 2 uses the modulus operator %. This means that the remainder of $int / 2 will be returned. When an even integer is divided by 2, there is never a remainder, so the result is 0. An odd integer divided by 2 has a remainder of 1. So if $int % 2 == 0, $int must be even, and this statement is true, so "$result = $int % 2 == 0 ? true : false" will assign true, the first value after the ?, to $result.

The function then returns $result as either true or false.



$int = 23;

if ( is_even($int) ) {

echo "$int is an even number.";

} else {

echo "$int is an odd number.";

}


The above will output "23 is an odd number."

There are some decimal numbers that will divide by 2 without a remainder, so this function needs to only accept integer values. You could change the trigger_error() if you to accept decimals and return false. Just get rid of "trigger_error(XXXX);" and put "return false;"

Good luck,

Jason

bluewalrus
02-01-2009, 03:50 PM
OOO Thanks a bunch for the explanation and the code.

Twey
02-01-2009, 04:33 PM
Uhm... better written as:
function is_even($n) {
return $n % 2 === 0;
}Of course, then it starts to seem a bit pointless to have a function for that at all...

'Even' means 'divides exactly by 2'. For any numbers n and m, if n divides exactly by m, then n % m === 0: there is no remainder left over after division.

JasonDFR
02-01-2009, 05:22 PM
Yeah that is concise. My goal was to make things crystal clear and provide a great explanation so I dragged it out a bit. I prefer to be talked to like a two year old when I am trying to learn something.

I disagree about it being pointless to have a function for this (as long as you are testing for even numbers more than one spot in your application or site). If you load any config file or function file with every page load, throw that function on there. "is_even()" is easier to code and most importantly understand. And it falls right in line with php functions like is_int, is_string, is_numeric, etc.

Twey
02-01-2009, 05:59 PM
I think foo % 2 === 0 is as much of an idiom in programming as are things like for loops. Nobody could really misunderstand it, and it truly is a very simple expression. I'm usually all for sectioning everything possible off into functions, but this isn't much above
function post_increment(&$n) {
return $n++;
}and
function add($a, $b) {
return $a + $b;
}

djr33
02-01-2009, 10:00 PM
Depending on the context and how often you write it in a certain program, it can be useful to have a function rather than any other sort of symbols in a program. If you use it in if statements, for example, it looks a lot simpler when skimming to have if(is_even($x)) than to write if($x%2===0), even though the result is the same. If you're planning to put that in 100 places in a program then I think it makes sense to use is_even rather than %...===0. Of course if you're only using it once, that's rather silly. Much like naming certain properties (such as in CSS) with a reasonable name for their use, a well-suitably named function can make the code appear a lot smoother than a bunch of math symbols, much like CSS should not contain a number of properties named "prop1," "prop2," ... "propn."

Twey
02-01-2009, 10:28 PM
On the contrary: if ($x % === 0) is exactly the sort of place where there is no point in having a function. It is useful in languages that don't have a way of treating an operator as a function to wrap operators in functions if one is intending to pass them to higher-order functions, but apart from that there is no reason.

What you suggest, functions as documentation, is again usually a good idea, but this particular instance is so common that, like addition, there's simply no gain. Any programmer worth their salt will recognise it instantly.

Nile
02-01-2009, 10:41 PM
I agree with Twey.
Once you get to something so short such as:


return $x % 2 === 0;

It is unreasonable for a function, and it is completely comprehensible.

Schmoopy
02-01-2009, 10:45 PM
Just for clarity, the three equals signs (===) do mean that both operands have to be the same type as well as equalling the same value right?

For example:

1 == "1" // Would return true?

and

1 === "1" // Would return false?

Snookerman
02-01-2009, 10:54 PM
That is correct, here's more info:
http://devzone.zend.com/node/view/id/626#Heading4 (http://devzone.zend.com/node/view/id/626#Heading4)
http://www.php.net/manual/en/language.operators.comparison.php (http://www.php.net/manual/en/language.operators.comparison.php)

Nile
02-01-2009, 10:55 PM
Correct.


<?php
function single($n) {
return $n == 5;
}
function dbl($n) {
return $n === 5;
}
echo single('5');
echo dbl('5');
?>

bluewalrus
02-02-2009, 03:40 AM
So, I did something wrong here the even function was working but then it stopped after I added in a check for a new month and it now brings up the only accepts integers error.

Maybe too many date functions in on process (I have 4 date functions)? I'm using php4 also if that makes a difference.

I'm switching servers in the next few days anyone know if a small orange company is good or now of a good one to use? I have 3 domains I need to swap over. My current server IX webhosting has crashed my domains 3+ times today and denies it's happening (2 techs, 1 said he knew) so I can't stay with them.

It also did occur to me that I would need a way to get the php to write even when the page was not loaded so that at the new month point it would write regardless of if a visitor was on the page at 11:59:59 or not.



$date2 = date('m.j.y');
function is_even($int) {
if ( !is_int($int) ) {
trigger_error('The is_even() function only accepts integers.', E_USER_WARNING);
return;
// Or get rid of trigger_error(xxx) and replace it with: return false;
// to accept any input. This will cause odd numbers and decimals
// to return false.

}

$result = $int % 2 == 0 ? true : false;

return $result;

}
$int = date('j');
if ( is_even($int) ) {
$first = '<tr class="even"><td>';
} else {
$first = '<tr class="odd"><td>';
}
$newmonth = date('m.d H:i:s');
if ($newmonth == "1/31 23:59:59" || $newmonth == "2.28 23:59:59" || $newmonth == "2.29 23:59:59" || $newmonth == "3.31 23:59:59" || $newmonth == "4.30 23:59:59" || $newmonth == "5/31 23:59:59" || $newmonth == "6/30 23:59:59" || $newmonth == "7/31 23:59:59" || $newmonth == "8/30 23:59:59" || $newmonth == "9/30 23:59:59" || $newmonth == "10/31 23:59:59" || $newmonth == "11/30 23:59:59" || $newmonth == "12/31 23:59:59") {
$who = "<tr><td class=\"row\" colspan=\"3\"> NEW MONTH</td></tr>" . $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>\n";
} else {
$who = $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>\n";
}
$where = str_replace("</table>", $who, $red);
$logger = fopen($info, "w+");
fwrite($logger, $where);
fclose($logger);

Thanks for any ideas and thoughts you can offer with these questions as well:).

Twey
02-02-2009, 03:13 PM
Probably you had a string like '2' or a float like 2.4. You don't actually want to only accept integers here, because of PHP's weak typing: just accept whatever you're given. Remove that clause from the function entirely.
$result = $int % 2 == 0 ? true : false;

return $result; is just pleonastically redundant and redundantly pleonastic, and can be written return $int % 2 == 0;.

JasonDFR
02-02-2009, 04:29 PM
$int = date('j');

The date() function returns a string. So you have two options, either make, $int an integer before using it in the even() function, or change the function.

You can make the result of the date function an integer by doing this:


$int = (int) date('j'); // typecast date('j') to an integer

Or, you can use Twey's function, but remember it will accept strings and return true.




// $int = "this is a string"; will return true

function is_even($int) {

return $int % 2 == 0;

}

bluewalrus
02-03-2009, 12:43 AM
Oh yea that makes sense thanks again.

Twey I just saw your response, won't the date always be a whole integer?

Twey
02-03-2009, 06:27 AM
No — in fact, as we've just seen, it'll always be a string.

bluewalrus
02-18-2009, 03:03 AM
Is there a command to make a html file like mkdir? I know wordpress does it and other cms's but I don't want to go threw all those files or if someone know's what file actually does the work in a free cms program that i could look through. Thanks for any ideas on this one as well.

Twey
02-18-2009, 03:31 AM
You want to make a directory? Yes, it's called mkdir (http://www.php.net/mkdir)().

bluewalrus
02-18-2009, 04:27 AM
No make an html file or a text file from php only like the mkdir() but maybe mkhtml()? mktxt()?

Nile
02-18-2009, 04:35 AM
How about fopen() (http://us.php.net/manual/en/function.fopen.php)?

bluewalrus
02-18-2009, 04:40 AM
That hasn't worked for me. I don't know if thats cause I'm on php4 or for another reason.

Nile
02-18-2009, 12:56 PM
Install PHP5.

bluewalrus
02-18-2009, 02:04 PM
my server wont or can't install it for me until I buy another plan and then they said they will have to take my sites down for 2 weeks at the most.

Twey
02-18-2009, 04:48 PM
Your host sucks — get another. PHP4 shouldn't really be supplied with any plans any more, especially as the only option, and a transfer to another plan should not result in downtime if done properly.
No make an html file or a text file from php only like the mkdir() but maybe mkhtml()? mktxt()? A file is a file is a file, unless it's a special file. A directory isn't a file, though. The only difference between a text file, an HTML file, and a JPEG file is what you put in it.

bluewalrus
02-18-2009, 11:37 PM
Any recommendations for servers? I have ix webhosting right now and I have 3 domains on it. There's about 1gb of space used.

So the fopen should work once i get php5?

Nile
02-19-2009, 12:17 AM
File_put_contents:


<?php
file_put_contents("file.html","<b>Hello</b>");
?>

Twey
02-19-2009, 01:46 AM
I don't know about fopen() — that should work in PHP4 too. Check you haven't made some elementary error (path name? permissions?).

file_put_contents() is only available in PHP5.

A Small Orange (http://asmallorange.com/services/hosting/) has lots of features, but I hear that iWeb (http://iweb.com/web-hosting/compare/) is very good, and offers better deals on space. Both provide PHP5. I'd be disinclined to go with iWeb for the sole reason that they need to be taught that the definition of an 'all-inclusive package' is that it, well, includes everything. Perhaps I'm overly picky. :p

Nile
02-19-2009, 01:47 AM
Make sure that the folder is writable, and use the "x" or "x+" too!

Aha, guess it is. ;)

bluewalrus
02-19-2009, 03:31 AM
I'm going to have to assume I'm making some mistake then that's usually the case. Here is my code (I just rewrote this to double check and still no luck):


<?php
$create = fopen("newfile1asdfghjkl.txt", "x");
chmod($create, 0777) == true;
$contents = "This is text";
fwrite($create, $contents);
fclose($create);
?>

x error message:

Warning: chmod() [function.chmod]: No such file or directory in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 11

x+ error message:

Warning: fopen(newfile1asdfghjkl.txt) [function.fopen]: failed to open stream: File exists in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 10

Warning: chmod() [function.chmod]: No such file or directory in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 11

Warning: fwrite(): supplied argument is not a valid stream resource in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 13

Warning: fclose(): supplied argument is not a valid stream resource in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 14

The <?php is line 9 and ?>is line 15 Thanks for any more ideas you can offer here. I think I'll go with the orange server.

Nile
02-19-2009, 03:51 AM
You can't change the mod of the file before its created!

Twey
02-19-2009, 04:12 AM
Well, here's the obvious:
$create = fopen("newfile1asdfghjkl.txt", "x");
chmod($create, 0777) == true;

The fopen (http://www.php.net/fopen)() fails because the file exists already — that's what 'x' (like 'x+') does. If you want to write to an existing file, use one of the 'a' (append) or 'w' (overwrite) family of flags.

chmod (http://www.php.net/chmod)() works with a filename, but you've given it a resource handle, so it's looking for a file named '5' or something (resource handles are long integers). What exactly is that == true supposed to do? It's redundant.

bluewalrus
02-19-2009, 04:27 AM
IT WORKED!!
wahoo

I don't know why it needs 2 = but it does. I tried it with 1 and that was the error then i put the second one and bamo I have a file created. Thanks.

Working code...


<?php
$name = "newfile1asdfghjkl.txt";
$create = fopen($name, "a");
chmod($name, 0777) == true;
$contents = "This is text";
fwrite($create, $contents);
fclose($create);
?>

Twey
02-19-2009, 05:04 AM
You can remove it entirely. Just chmod($name, 0777); should suffice. All you're doing there is comparing the result to true and then discarding the result of the comparison.

bluewalrus
02-24-2009, 12:16 AM
I have another one...

How can I not have quotes auto escaped. This is what the code is being written as in my text files.


<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/c7BS8adXyBE&hl=en&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/c7BS8adXyBE&hl=en&fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>

That all comes from youtube but the =\" dont belong should just be =" or if not getting it to not write those how can i get it to ignore them when reading it with fread? Thanks for any ideas on this as well.

Twey
02-24-2009, 12:24 AM
Nothing to do with your file reading and writing. It sounds like magic_quotes_gpc. See http://www.php.net/magic_quotes.

bluewalrus
02-24-2009, 01:25 AM
ah ha you found it again thanks

I sent this to my sever and presto chango fixed. http://www.php.net/manual/en/security.magicquotes.disabling.php