php-general Digest 26 Nov 2007 11:44:02 -0000 Issue 5148

Topics (messages 265011 through 265021):

Re: URL Parsing...
        265011 by: Chris

How to ask "if private IP"?
        265012 by: Ronald Wiplinger
        265013 by: Andrés Robinet
        265014 by: mike
        265021 by: Jochem Maas

session_destroy AND reload a page
        265015 by: Ronald Wiplinger
        265016 by: Chris

question regarding type hinting parameters of php functions
        265017 by: Dirk Thomas / 4wd media
        265018 by: T.Lensselink

Session cookie doesn't work
        265019 by: Teck
        265020 by: metastable

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Richard Heyes wrote:
well if you take a string (filename) and wish to change the end of it somone then I don't think str_replace() is the correct function. what's to say a script
doesn't exist called 'my.cfm.php'?

How does this:

/\.cfm$/

take into account that?

$ in regex's means 'end of string' - so it will only match .cfm at the very end of the string.

--
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
I use $aa=$_SERVER["REMOTE_ADDR"];

and

if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
    $aa="61.64.101.101";    // for testing put in a public IP   
}


However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
and 172.??.x.x). How can I do that simple?

bye

Ronald

--- End Message ---
--- Begin Message ---
$remoteIP = $_SERVER['REMOTE_ADDR'];
$parts = explode('.', $remoteIP); // Get IP bytes in an array
$isPrivateIP = false;

/* single class A */
$classA = $parts[0] == 10;
/* 16 contiguous class Bs */
$classB = $parts[0] == 172 && $parts[1] >= 16 && $parts[1] <= 31;
/* 256 contiguous class Cs */
$classC = $parts[0] == 192 && $parts[1] == 168;
If ($classA || $classB || $classC || $remoteIP == '127.0.0.1') {
        $isPrivateIP = true;
}

I would include the three tests inline (class A, B, C) inside the "if"
condition to get some performance benefit for short circuit boolean
evaluation (meaning that once one condition is true, the parser will stop
checking the others).

Rob

Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308
| TEL 954-607-4207 | FAX 954-337-2695 | 
Email: [EMAIL PROTECTED]  | MSN Chat: [EMAIL PROTECTED]  |  SKYPE:
bestplace |  Web: bestplace.biz  | Web: seo-diy.com

> -----Original Message-----
> From: Ronald Wiplinger [mailto:[EMAIL PROTECTED]
> Sent: Sunday, November 25, 2007 9:52 PM
> To: PHP General list
> Subject: [PHP] How to ask "if private IP"?
> 
> I use $aa=$_SERVER["REMOTE_ADDR"];
> 
> and
> 
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
>     $aa="61.64.101.101";    // for testing put in a public IP
> }
> 
> 
> However, I would like to cover all private IPs (192.168.x.x and
> 10.x.x.x
> and 172.??.x.x). How can I do that simple?
> 
> bye
> 
> Ronald

--- End Message ---
--- Begin Message ---
i did this once

$ip = sprintf("%u",intval(ip2long($_SERVER['REMOTE_ADDR'])));

and then did checks to see if it was between the ranges for 10.0.0.0,
192.168 etc...

apparently i lost that code. but it was pretty simple.
http://www.faqs.org/rfcs/rfc1918.html

     10.0.0.0        -   10.255.255.255  (10/8 prefix)
     172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
     192.168.0.0     -   192.168.255.255 (192.168/16 prefix)

and then what, 224.0.0.0-255.255.255.255? and not 0.0.0.0.

i had it all figured out... just print out what the 10.0.0.0 is in
integer, and 10.255.255.255 and such, and then just check if the
integer is between any of the "invalid" ranges. the integer math
should be very fast.

--- End Message ---
--- Begin Message ---
Ronald Wiplinger wrote:
> I use $aa=$_SERVER["REMOTE_ADDR"];
> 
> and
> 
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
>     $aa="61.64.101.101";    // for testing put in a public IP   
> }
> 
> 
> However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> and 172.??.x.x). How can I do that simple?

probably not the best or fastest way to do it but here is what I use:

function isPrivateIP($theip)
{
    foreach (array("10.0.0.0/8",
                   "172.16.0.0/12",
                   "192.168.0.0/16") as $subnet)
    {
        list($net, $mask) = explode('/', $subnet);
        if(isIPInSubnet($theip, $net, $mask))
            return true;
    }

    return false;
}

function isIPInSubnet($ip, $net, $mask)
{
    $firstpart  = substr(str_pad(decbin(ip2long($net)), 32, "0", STR_PAD_LEFT) 
,0 , $mask);
    $firstip    = substr(str_pad(decbin(ip2long($ip)), 32, "0", STR_PAD_LEFT), 
0, $mask);

    return (strcmp($firstpart, $firstip) == 0);
}


> 
> bye
> 
> Ronald
> 

--- End Message ---
--- Begin Message ---
If my user wants to logout, I want that the session will be destroyed
and that he must start with the first page again (index.php) and a new
session.

Whatever I try he always gets the old sessions or he does not come to
the first page.

How can I solve that?

bye

Ronald

--- End Message ---
--- Begin Message ---
Ronald Wiplinger wrote:
If my user wants to logout, I want that the session will be destroyed
and that he must start with the first page again (index.php) and a new
session.

Whatever I try he always gets the old sessions or he does not come to
the first page.

What code do you have?

--
Postgresql & php tutorials
http://www.designmagick.com/

--- End Message ---
--- Begin Message ---
Hi,

i have tried a current snapshot of PHP 5.3 and have a question regarding type hinting.

For example when using the function
"array_slice(array $array, int $offset, int $length)"
with a non-integer length parameter, what is the desired behavior?

When calling
  "array_slice($array, 0, (float)2);"
  the resulting array is EMPTY.
When using the right type
  "array_slice($array, 0, (int)2);"
  it works as expected.

Shouldn't there be a notice/warning than just a wrong return value?
In my case there is neither a warning nor does it work as expected.
(perhaps it do something wrong?)

Of course i can use int's, but in my opinion either a warning should be given or the function should gracefully handle the wrong typed parameter.

Thank you for any feedback,
Dirk

--- End Message ---
--- Begin Message ---
On Mon, 26 Nov 2007 10:29:40 +0100, Dirk Thomas / 4wd media
<[EMAIL PROTECTED]> wrote:
> Hi,
> 
> i have tried a current snapshot of PHP 5.3 and have a question regarding
> type hinting.
> 
> For example when using the function
> "array_slice(array $array, int $offset, int $length)"
> with a non-integer length parameter, what is the desired behavior?
> 
> When calling
>    "array_slice($array, 0, (float)2);"
>    the resulting array is EMPTY.
> When using the right type
>    "array_slice($array, 0, (int)2);"
>    it works as expected.
> 
> Shouldn't there be a notice/warning than just a wrong return value?
> In my case there is neither a warning nor does it work as expected.
> (perhaps it do something wrong?)
> 
> Of course i can use int's, but in my opinion either a warning should be
> given or the function should gracefully handle the wrong typed parameter.
> 
> Thank you for any feedback,
> Dirk
> 

I think there should at least be a notice. It's propably better to ask this
on the internals list.

--- End Message ---
--- Begin Message ---
Hi,

I'm working to use cookie to maintain session data across multiple pages in PHP4.42. I have the following two scripts, where I think the second file outputs a data in a session variable.

file 1 - test_1.php #####################

<?php
session_set_cookie_params(360 * 24 * 3600);
session_start();

$_SESSION['name'] = "Kevn";
?>

<a href="test_2.php?<php? echo SID ?>">Go next</a>

##########################################

file 2 - test_2.php ######################


<?php

session_start();
echo $_SESSION['name']; // Here I expect to show the word "Kevin"

?>


##########################################

phpinfo() tells me that
* Session Support enabled
* session.auto_start Off
* session.bug_compat_42 On
* session.bug_compat_warn On
* session.cache_expire 180
* session.cache_limiter nocache
* session.cookie_domain no value
* session.cookie_path /
* session.cookie_secure Off
* session.entropy_file no value
* session.entropy_length 0
* session.gc_divisor 100
* session.gc_maxlifetime 1440
* session.gc_probability 0
* session.name PHPSESSID
* session.referer_check no value
* session.save_handler files
* session.save_path /var/lib/php4
* session.use_cookies On
* session.use_only_cookies On
* session.use_trans_sid On

What am I missing?

In file 1, I also tried "<a href="test_2.php>Go next</a> .
I've been learning PHP using various books and websites. Still I don't solve this issue.

Any help would be apprecaited.

--- End Message ---
--- Begin Message ---
Hey,

Your session will expire, regardless of the call to session_set_cookie_params. If you're looking to propagate a session across time, you should look at keeping your session data in a database (google: session_set_save_handler, there's a good tutorial on the zend site), then calling a table row based on a cookie you set independently of the session.

Based on the configuration you presented, I don't see why the following code shouldn't give you the expected results:

## test1.php  ##
<?php
session_start();
$_SESSION['name'] = 'Kevin';
?>
<a href="test2.php">test2</a>
###########

## test2.php ##
<?php
session_start();
echo $_SESSION['name'];
// If the above doesn't work, try doing a print_r($_SESSION) and let us know what that brings up.
?>
###########

If for some reason I am not aware off, your typical setup doesn't automatically save the session before exiting the script, you could try ending your scripts with:
<?php
session_write_close();
?>


As a general tip, I suggest you set your error_reporting to E_ALL on your development machine. That would print out a notice when a certain index or key is not set in an array.


best regards,

Stijn


Teck wrote:
Hi,

I'm working to use cookie to maintain session data across multiple pages in PHP4.42. I have the following two scripts, where I think the second file outputs a data in a session variable.

file 1 - test_1.php #####################

<?php
session_set_cookie_params(360 * 24 * 3600);
session_start();

$_SESSION['name'] = "Kevn";
?>

<a href="test_2.php?<php? echo SID ?>">Go next</a>

##########################################

file 2 - test_2.php ######################


<?php

session_start();
echo $_SESSION['name']; // Here I expect to show the word "Kevin"

?>


##########################################

phpinfo() tells me that
* Session Support enabled
* session.auto_start Off
* session.bug_compat_42 On
* session.bug_compat_warn On
* session.cache_expire 180
* session.cache_limiter nocache
* session.cookie_domain no value
* session.cookie_path /
* session.cookie_secure Off
* session.entropy_file no value
* session.entropy_length 0
* session.gc_divisor 100
* session.gc_maxlifetime 1440
* session.gc_probability 0
* session.name PHPSESSID
* session.referer_check no value
* session.save_handler files
* session.save_path /var/lib/php4
* session.use_cookies On
* session.use_only_cookies On
* session.use_trans_sid On

What am I missing?

In file 1, I also tried "<a href="test_2.php>Go next</a> .
I've been learning PHP using various books and websites. Still I don't solve this issue.

Any help would be apprecaited.


--- End Message ---

Reply via email to