Am I using the PHP.js strpbrk function properly? - php

I have the following JS function:
function validatePass()
{
var oldPass = document.getElementById("oldpass").value;
var newPass1 = document.getElementById("newpass1").value;
var newPass2 = document.getElementById("newpass2").value;
if(strpbrk(newPass1,"abcdefghijklmnopqrstuvwxyz") != false)
{
document.getElementById("ContainsAtLeastOneLowercaseLetter").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneLowercaseLetter").innerHTML = "<span id=\"red\">Not Met</span>";
}
if(strpbrk(newPass1,"ABCDEFGHIJKLMNOPQRSTUVWXYZ") != false)
{
document.getElementById("ContainsAtLeastOneUppercaseLetter").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneUppercaseLetter").innerHTML = "<span id=\"red\">Not Met</span>";
}
if(strpbrk(newPass1,"1234567890") != false)
{
document.getElementById("ContainsAtLeastOneNumber").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneNumber").innerHTML = "<span id=\"red\">Not Met</span>";
}
}
That code makes a call to the PHP.js function strpbrk to determine if newPass1 contains acceptable data. In PHP, strpbrk searches to see if the haystack contains any of the chars in the char_list. However, in this JS implementation, it searches for all of the chars. How can I make it behave the way I want to?
TIA.

I would suggest using regex instead of strpbrk.
function validatePass()
{
var oldPass = document.getElementById("oldpass").value;
var newPass1 = document.getElementById("newpass1").value;
var newPass2 = document.getElementById("newpass2").value;
if(/[a-z]/.test(newpass1))
{
document.getElementById("ContainsAtLeastOneLowercaseLetter").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneLowercaseLetter").innerHTML = "<span id=\"red\">Not Met</span>";
}
if(/[A-Z]/.test(newpass1))
{
document.getElementById("ContainsAtLeastOneUppercaseLetter").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneUppercaseLetter").innerHTML = "<span id=\"red\">Not Met</span>";
}
if(/[0-9]/.test(newpass1))
{
document.getElementById("ContainsAtLeastOneNumber").innerHTML = "<span id=\"green\">Met</span>";
}
else
{
document.getElementById("ContainsAtLeastOneNumber").innerHTML = "<span id=\"red\">Not Met</span>";
}
}

Related

PHP domain availability script exec() function alternative

I want to install a php whois domain availability check script on my website which I found somewhere.
It has a whois_class.php file where it allows my to switch between "win" & "linux" mode.
When I switch on "win" mode it works fine in my localhost as I am using Windows OS.
But when I upload it to the server it stops working as my server is Linux Server.
So I have to switch it to "linux" mode and re-upload whois_class.php. This was expected to work fine but for linux mode the developer of the script has used exec() function which my host has disabled for security purposes.
So, the only option I guess is if I change my server platform from Linux to Windows. But, that will be a mess.
I was looking for a way how I can use this script to run smoothly on my linux server just as it runs on my localhost in Windows OS. Any change or modification to the script to make it run will be appreciated.
whois_class.php :
<?php
class Whois_domain {
var $possible_tlds;
var $whois_server;
var $free_string;
var $whois_param;
var $domain;
var $tld;
var $compl_domain;
var $full_info;
var $msg;
var $info;
var $os_system = "win"; // switch between "linux" and "win"
function Whois_domain() {
$this->info = "";
$this->msg = "";
}
function process() {
if ($this->create_domain()) {
if ($this->full_info == "yes") {
$this->get_domain_info();
} else {
if ($this->check_only() == 1) {
$this->msg = "<p style='font-size: 16px'>The domain name: <font color='#000'>".$this->compl_domain."</font> is <font color='#2ec62e'>available</font>.</p>";
return true;
} elseif ($this->check_only() == 0) {
$this->msg = "<p style='font-size: 16px'>The domain name: <font color='#000'>".$this->compl_domain."</font> is <font color='red'>registered</font>.</p>";
return false;
} else {
$this->msg = "<p style='font-size: 16px'>There was something wrong, try it again.</p>";
}
}
} else {
$this->msg = "<p style='font-size: 16px'>Only letters, numbers and hyphens (-) are valid!</p>";
}
}
function check_entry() {
if (preg_match("/^([a-z0-9]+(\-?[a-z0-9]*)){2,63}$/i", $this->domain)) {
return true;
} else {
return false;
}
}
function create_tld_select() {
$menu = "<select id=\"tld\" name=\"tld\" style=\"margin-left:0; width: 100px; margin-top: 20px; border-radius: 5px\">\n";
foreach ($this->possible_tlds as $val) {
$menu .= " <option value=\"".$val."\"";
$menu .= (isset($_POST['tld']) && $_POST['tld'] == $val) ? " selected=\"selected\">" : ">";
$menu .= $val."</option>\n";
}
$menu .= "</select>\n";
return $menu;
}
function create_domain() {
if ($this->check_entry()) {
$this->domain = strtolower($this->domain);
$this->compl_domain = $this->domain.".".$this->tld;
return true;
} else {
return false;
}
}
function check_only() {
$data = $this->get_whois_data();
if (is_array($data)) {
$found = 0;
foreach ($data as $val) {
if (preg_match('/'.$this->free_string.'/', $val)) {
$found = 1;
}
}
return $found;
} else {
$this->msg = "<p style='font-size: 16px'>Error, please try it again.</p>";
}
}
function get_domain_info() {
if ($this->create_domain()) {
$data = ($this->tld == "nl") ? $this->get_whois_data(true) : $this->get_whois_data();
//print_r($data);
if (is_array($data)) {
foreach ($data as $val) {
if (eregi($this->free_string, $val)) {
$this->msg = "<p style='font-size: 16px'>The domain name: <font color='#000'>".$this->compl_domain."</font> is <font color='#2ec62e'>available</font>.</p>";
$this->info = "";
break;
}
$this->info .= $val;
}
} else {
$this->msg = "<p style='font-size: 16px'>Error, please try it again.</p>";
}
} else {
$this->msg = "<p style='font-size: 16px'>Only letters, numbers and hyphens (-) are valid!</p>";
}
}
function get_whois_data($empty_param = false) {
// the parameter is new since version 1.20 and is used for .nl (dutch) domains only
if ($empty_param) {
$this->whois_param = "";
}
if ($this->tld == "de") $this->os_system = "win"; // this tld must be queried with fsock otherwise it will not work
if ($this->os_system == "win") {
$connection = #fsockopen($this->whois_server, 43);
if (!$connection) {
unset($connection);
$this->msg = "<p style='font-size: 16px'>Can't connect to the server!</p>";
return;
} else {
sleep(2);
fputs($connection, $this->whois_param.$this->compl_domain."\r\n");
while (!feof($connection)) {
$buffer[] = fgets($connection, 4096);
}
fclose($connection);
}
} else {
$string = "whois -h ".$this->whois_server." \"".$this->whois_param.$this->compl_domain."\"";
$string = str_replace (";", "", $string).";";
exec($string, $buffer);
}
if (isset($buffer)) {
//print_r($buffer);
return $buffer;
} else {
$this->msg = "<p style='font-size: 16px'>Can't retrieve data from the server!</p>";
}
}
}
?>
The problem is here
if ($this->os_system == "win") {
$connection = #fsockopen($this->whois_server, 43);
if (!$connection) {
unset($connection);
$this->msg = "<p style='font-size: 16px'>Can't connect to the server!</p>";
return;
} else {
sleep(2);
fputs($connection, $this->whois_param.$this->compl_domain."\r\n");
while (!feof($connection)) {
$buffer[] = fgets($connection, 4096);
}
fclose($connection);
}
} else {
$string = "whois -h ".$this->whois_server." \"".$this->whois_param.$this->compl_domain."\"";
$string = str_replace (";", "", $string).";";
exec($string, $buffer);
}
Disabled functions by my host
system,exec,shell_exec,passthru,popen,proc_open,pcntl_exec,highlight_file,show_source,symlink,link,posix_getpwuid,posix_getpwnam,posix_getgrgid,posix_getgrnam,posix_kill,posix_mkfifo,posix_getrlimit
There are plenty of functions related to program execution. However you should check which other functions are disabled on your setup:
var_dump(ini_get("disable_functions"));

php whois stopped working on another server

I am using this whois class, it works fine on one server but it does not work properly on another server with the same PHP version 5.4, on first server it returns domain name status correctly, but on the other one it returns just one status: "domain name is not available" while the domain name is actually available.
<?
class Whois_domain {
var $possible_tlds;
var $whois_server;
var $free_string;
var $whois_param;
var $domain;
var $tld;
var $compl_domain;
var $full_info;
var $msg;
var $info;
var $os_system = "linux"; // switch between "linux" and "win"
function Whois_domain() {
$this->info = "";
$this->msg = "";
}
function process() {
if ($this->create_domain()) {
if ($this->full_info == "yes") {
$this->get_domain_info();
} else {
if ($this->check_only() == 1) {
$this->msg = "The domain name: <b>".$this->compl_domain."</b> is free.";
return true;
} elseif ($this->check_only() == 0) {
$this->msg = "The domain name: <b>".$this->compl_domain."</b> is not available";
return false;
} else {
$this->msg = "There was something wrong, try it again.";
}
}
} else {
$this->msg = "Only letters, numbers and hyphens (-) are valid!";
}
}
function check_entry() {
if (preg_match("/^([a-z0-9]+(\-?[a-z0-9]*)){2,63}$/i", $this->domain)) {
return true;
} else {
return false;
}
}
function create_tld_select() {
$menu = "<select name=\"tld\" style=\"margin-left:0;\">\n";
foreach ($this->possible_tlds as $val) {
$menu .= " <option value=\"".$val."\"";
$menu .= (isset($_POST['tld']) && $_POST['tld'] == $val) ? " selected=\"selected\">" : ">";
$menu .= $val."</option>\n";
}
$menu .= "</select>\n";
return $menu;
}
function create_domain() {
if ($this->check_entry()) {
$this->domain = strtolower($this->domain);
$this->compl_domain = $this->domain.".".$this->tld;
return true;
} else {
return false;
}
}
function check_only() {
$data = $this->get_whois_data();
if (is_array($data)) {
$found = 0;
foreach ($data as $val) {
if (eregi($this->free_string, $val)) {
$found = 1;
}
}
return $found;
} else {
$this->msg = "Error, please try it again.";
}
}
function get_domain_info() {
if ($this->create_domain()) {
$data = ($this->tld == "nl") ? $this->get_whois_data(true) : $this->get_whois_data();
if (is_array($data)) {
foreach ($data as $val) {
if (eregi($this->free_string, $val)) {
$this->msg = "The domain name: <b>".$this->compl_domain."</b> is free.";
$this->info = "";
break;
}
$this->info .= $val;
}
} else {
$this->msg = "Error, please try it again.";
}
} else {
$this->msg = "Only letters, numbers and hyphens (-) are valid!";
}
}
function get_whois_data($empty_param = false) {
// the parameter is new since version 1.20 and is used for .nl (dutch) domains only
if ($empty_param) {
$this->whois_param = "";
}
if ($this->tld == "de") $this->os_system = "win"; // this tld must be queried with fsock otherwise it will not work
if ($this->os_system == "win") {
$connection = #fsockopen($this->whois_server, 43);
if (!$connection) {
unset($connection);
$this->msg = "Can't connect to the server!";
return;
} else {
sleep(2);
fputs($connection, $this->whois_param.$this->compl_domain."\r\n");
while (!feof($connection)) {
$buffer[] = fgets($connection, 4096);
}
fclose($connection);
}
} else {
$string = "whois -h ".$this->whois_server." \"".$this->whois_param.$this->compl_domain."\"";
$string = str_replace (";", "", $string).";";
exec($string, $buffer);
}
if (isset($buffer)) {
//print_r($buffer);
return $buffer;
} else {
$this->msg = "Can't retrieve data from the server!";
}
}
}
?>
I changed files permissions and php versions on the other server, but still the same.
This has been solved, it was fsockopen restrictions on the server, it is fine now.

Can't redirect to another page

I am using this Codeigniter function
redirect('home');
but it causes me this browser error: ERR_TOO_MANY_REDIRECTS
The page can't even get loaded.
My whole function looks like this:
public function establish($target) {
if ($target == 'sender' || $target == 'receiver') {
$this->validate($target);
if ($this->validated) {
if ($target == 'sender') {
$this->sender_db = $this->session->userdata('sender_db');
$this->sender_host = $this->session->userdata('sender_host');
$this->sender_user = $this->session->userdata('sender_user');
$this->sender_pw = $this->session->userdata('sender_pw');
if ($this->load->database($this->define_database('sender'))) {
$this->err_receiver = 0;
return $this->load->database($this->define_database('sender'), TRUE);
}
else {
$this->err_sender = 1;
}
}
elseif ($target == 'receiver') {
$this->receiver_db = $this->session->userdata('receiver_db');
$this->receiver_host = $this->session->userdata('receiver_host');
$this->receiver_user = $this->session->userdata('receiver_user');
$this->receiver_pw = $this->session->userdata('receiver_pw');
if ($this->load->database($this->define_database('receiver'))) {
$this->err_receiver = 0;
return $this->load->database($this->define_database('receiver'), TRUE);
}
else {
$this->err_receiver = 1;
}
}
else {
echo 'Error: illegal parameter. Please use sender or receiver instead.';
}
}
else {
echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
exit;
}
redirect('home');
}
else {
echo 'Error: illegal parameter. Please use sender or receiver instead.';
}
}
What am I doing wrong?
I figured out the issue. I was running through an endless loop. Two pages were forwarding each other over and over again.

count conditional output in foreach loop

I have foreach loop like this
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
}
else
{
$msg = "No";
}
}
How can I count number of "Yes" and "No" generated by "if statement" outside "foreach loop"?
Try:
$yes = 0;
$no = 0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$yes += 1;
}
else
{
$no += 1;
}
}
echo "Yes " . $yes . "<br>" . "No " . $no;
Inside 'if' statement you can try this
$yesCount = 0;
$noCount = 0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$yesCount++;
$msg = "Yes";
}
else
{
$noCount++;
$msg = "No";
}
}
But i am not sure whether it can be used outside.
$yesCount = 0;
$noCount = 0;
foreach($destinations as $destination) {
if($destination=="abc") {
$msg = "Yes";
$yesCount = $yesCount + 1;
}
else {
$msg = "No";
$noCount = $noCount + 1;
}
}
echo $yesCoynt . " --- " . $noCount;
Just try with the following example :
<?php
$destinations = array('abc','def','ghi');
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
$msg_yes_counter[]= "I'm in";
}
else
{
$msg = "No";
$msg_no_counter[]= "I'm in";
}
}
echo "YES -> My Count is :".count($msg_yes_counter);
echo "NO -> My Count is :".count($msg_no_counter);
?>
Create two flag variables and try this
$yesFlag=0;
$noFlag=0;
foreach($destinations as $destination)
{
if($destination=="abc")
{
$msg = "Yes";
$yesFlag++;
}
else
{
$msg = "No";
$noFlag++;
}
}
echo "no. of Yes:".yesFlag;
echo "no. of NO:".noFlag;
Use the array_count_values() function, no need for a loop at all then.
array_count_values($destinations);

Why is the return value of this PHP function zero for a long array?

When the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. Where is the problem?
<?php
if ($row['error'] == "")
{
$error = "0";
}
else
{
$error = $row['error'];
}
if ($row['error'] != "")
{
if (strlen($error) > 35)
{
$error = substr($row['error'],0,32) + "...";
}
else
{
$error = $row['error'];
}
}
?>
Change
$error = substr($row['error'],0,32) + "...";
to:
$error = substr($row['error'],0,32) . "...";
The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign
All this code is not necessary. The second condition is redundant, and it doubles the else condition from the above. Make it all with just these few lines of code:
<?php
$error = $row['error'];
if (strlen($error) > 35) {
$error = substr($row['error'],0,32) . "...";
}
?>
Because you check:
if(strlen($error) > 35) {
}

Categories