How to validate IPv4 and IPv6 with PHP [closed] - php

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;
}

Related

How to make an php file have an url for example: "example.com/testing/page.php?p=1 [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 8 days ago.
Improve this question
I tried things like
$query = parse_url($url, PHP_URL_QUERY);
// Returns a string if the URL has parameters or NULL if not
if ($query) {
$url .= '&category=1';
} else {
$url .= '?category=1';
echo "hi";
}
?>
but still i cant seem to find the answer.

How to make a php crawler to search particular string? [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 7 years ago.
Improve this question
How can I create a crawler in php which search for a particular string in a webpage and returns whether it is present or not?
Try this function I made, it takes in the pages URL and the string to look for in this URL's content.
<?php
var_dump(searchPage("http://google.com", "Tacos")); //False
var_dump(searchPage("http://google.com", "Google")); //True
function searchPage($url, $string){
$input = file_get_contents($url);
if (strpos($input,$string) !== false) {
return true;
}else{
return false;
}
}
?>
I hope this helped,
Sebastian

delete Persian's Chars of string in 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 7 years ago.
Improve this question
In PHP, i have strings , similar:
"www.mysite.com/fa/doc/report/67571/مطذح کردنو تت";
"www.mysite.com/fa/571/نهتال اهخع";
"www.mysite.com/fa/";
I want if there are Persian's Chars of string, delete them.
Output:
www.mysite.com/fa/doc/report/67571/
www.mysite.com/fa/571/
www.mysite.com/fa/
How can i do this?
You should try this code.
<?php
echo remove_persian("www.mysite.com/fa/doc/report/67571/مطذح کردنو تت");
function remove_persian($text)
{
return preg_replace('#[^a-zA-Z0-9./]#', '', $text);
}
This will output like this:
www.mysite.com/fa/doc/report/67571/
You can use a regex :
$clean = preg_replace('[^a-zA-Z\/\d\s:]', '', $url);

PHP If Then statement based on numeric range from field output [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 8 years ago.
Improve this question
I have a number field: <?php echo $entity->field_zip_code_name[0];?>
I want to create an if then statement based on the output number. Here is what I have:
if ($entity->field_areacode[0] = 92919,92923,94975,37783) {
<h3 style="font-size: 20px;"><strong>Area Code:</strong></h3>;
}
else {
NONE;
}
any help would be greatly appreciated.
Not sure what x is, if it's $entity->field_zip_code_name[0] then echo that:
if(in_array($entity->field_zip_code_name[0], [92919,92923,94975,37783])) {
//display x
}
If your PHP version doesn't support [] then use array():
if(in_array($entity->field_zip_code_name[0], array(92919,92923,94975,37783))) {
//display x
}

What's the best way to do IPv6 check in PHP? [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
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.

Categories