What's the best way to do IPv6 check in PHP? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Right now I'm using strcmp but that's not really optimal.
So, what's the best way to do it?

You can also use filter_var :
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
echo "IPV6 valid.";
} else {
echo "Not valid.";
}
You can find the list of filters here.

Use strcmp instead of strcpy. That must be the issue.

You can just check, if a : is in the ip-adress:
if (strpos($_SERVER["REMOTE_ADDR"],":") !== false) //....IPv6
else //....IPv4

Use inet_pton. Also works for ipv4:
function ip_validate($ip){
return inet_pton($ip) !== false;
}

$_SERVER["REMOTE_ADDR"] this is your buddy. It checks for the adress. I suppose the simplest way would be to check this address for a non-numeric sign. If it contains one, its a IPv6 address. Maybe there are better or more elegant ones, but ive never used it because no provider is using IPv6 yet.

Related

How to validate IPv4 and IPv6 with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Note! I can not use filter_var in my application. Only generic functions.
Perhaps a regex?
<?php
if (is_valid_ipv4($ip)) { ... }
else if (is_valid_ipv6($ip) { ... }
else { ... }
?>
You can just use inet_pton. It returns false if the IP is not a valid IPv6 or IPv4:
function validateIP($ip){
return inet_pton($ip) !== false;
}

Decimal number regular expression, no any . {dot or point } Using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers

How do I sanitize input on a URL/IP address field on my site? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm someone of a newbie. I'm not sure what the best way to prevent xss or injections on this field would be.
I have a field on my site that will take a user-submitted IP address or URL in order to complete a ping/traceroute test. I really only want to allow what HAS to be allowed for the thing to be functional. I have the whole thing working right now, but I'm not sure how to sanitize the input.
Basically, here's my function for ping. I don't know a LOT about output buffering, but it seemed like the easiest way to capture the output from the command.
function ping($domain){
echo "</div>";
$command = shell_exec("ping -c 3 $domain");
ob_start();
echo "<pre>$command</pre>";
$output = ob_get_clean();
echo "\n" . $output;
}
You could make use of PHP's built-in filtering with filter_var()
if($domain === filter_var($domain, FILTER_VALIDATE_URL) || $domain === filter_var($domain, FILTER_VALIDATE_IP)
{
// run your existing code
}
else
{
// invalid input
}
I'm sure something like the following should work well:
if(!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $domain) &&
!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/i', $domain))
die('Not a valid domain!');
Example here: http://phpfiddle.io/fiddle/843401527

In this case which is better in matching regular expression or parse_url [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Suppose we have the following link
$link = "http://www.mysite.com/bbbbbbb.flv?something";
And we want to matching it.
Upon this question how to match links that has extension #Thanks to all#
We have two ways regular expression and parse_url
So my question which is better in your point of view and for my website (such as fast better accurate ..etc)?
Method 1 regular expression
if (preg_match('/\.flv(\?|$)/i', $link)) {
echo 'It has flv';
} else {
echo 'It does not have';
}
Method 2 parse_url
$arr = parse_url($link);
$pathParts = pathinfo($arr['path']);
if ($pathParts['extension'] == 'flv')
echo "valid extension";
else
echo "invalid extension";
# Thanks
According to this publication, using the built in parse_url is much slower than preg_match. This is understandably the case, because the regular expression captures a single result, whereas parse_url captures and parses all components of the url.

Regular Expression Help for Date Validation - mm-dd-yyyy - PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if(preg_match('/^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/', '10-30-2013')){
echo 'true';
}
else {
echo 'false';
}
This not give me true. I think I'm wrong with regex. please tell how to correct this regex
I suggest not using regex at all for this -- date validation with regex is a surprisingly difficult thing to get right, due to the variations possible in the number of days in any given month.
Far better to simply use PHP's DateTime class to actually parse it into a date object. This will also validate it; if it doesn't meet the specified format, then it won't parse.
$dateObj = DateTime::CreateFromFormat('m-d-Y',$inputString);
See PHP manual page for CreateFromFormat().
You should use dedicate functions to parse date ie.:
if (strptime ('10-30-2013' , 'm-d-Y') !== false) {
echo 'true'.PHP_EOL;
} else {
echo 'true'.PHP_EOL;
}
Actually as the others confirmed, your regex works fine and returns true.
But as you made your code shrunken, I think the input string you're trying to show us isn't exactly just a date and it's within a string or maybe has trailing spaces!
So using ^ and $ at your regex will result in failure.

Categories