Jump to content

Improper form working in php


heeha

Recommended Posts

I am trying to code to but having some troubles. 


<?php
$siteErr="";
$site="";


if ($_SERVER["REQUEST_METHOD"] == "GET") {
   $site = trim_useless($_GET['url']);
  
}
 if (empty($_GET['url'])) {
     $site = "";
   } else {
     $site = trim_useless($_GET['url']);
     if (!preg_match("/\b(??:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$site)) {
       $siteErr = "Invalid URL"; 
     }
   }

function trim_useless($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="GET">
Enter URL : <input type="text" name="url">
<span class="error"> * <?php echo $siteErr;?></span>
Submit: <input type="submit" name="submit">
</form>
<div> 
<?php 
$site= dns_get_record($_GET['url'], DNS_MX);
foreach ($site as $in){
echo "<table>";
echo "<tr>";
echo "<th>Host</th>";
echo "<th>Class</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" .$in['host']. "</td>";
echo "<td>" .$in['class']. "</td>";
echo "</table>";
}
?>

 

1.

Warning: dns_get_record(): An unexpected server failure occurred. in /home/u992092914/public_html/b/test.php on line 48

Warning: Invalid argument supplied for foreach() in /home/u992092914/public_html/b/test.php on line 49

 

 

2. Form validation doesnt work. I mean even if i enter abc rather than some .com , it will still process the form.

 

3. No invalid Url gets displayed if display, doesnt stop form to get processed.

 

Link to comment
Share on other sites

 

 

2. Form validation doesnt work. I mean even if i enter abc rather than some .com , it will still process the form.

 

3. No invalid Url gets displayed if display, doesnt stop form to get processed.

It is processing the form regardless because you have not instructed PHP not to do that. PHP only does what you explicitly instruct it to do.

 

You should only retrieve/show the dns records for the url if a) a url has been submitted and b) the url passes your validation.

 

But your next problem is dns_get_record() does not take a url. It requires only a hostname (the domain). If you want to get the hostname from a url you will first want to call parse_url and grab the hostname segment, using  PHP_URL_HOST  flag. Also if you want to validate a url use filter_input using  FILTER_VALIDATE_URL  as the filter

 

Pseudo code for how to layout your code

<?php

if ($_GET['url'] exists)
{
     if(url is valid)
     {
          get hostname from url
          get dns record for hostname and save to a variable
     }
     else
     {
          output error, url is not valid
     }
}

?>

show form

<?php

if (have dns records)
{
    output dns records here
}

?>
Link to comment
Share on other sites

 

It is processing the form regardless because you have not instructed PHP not to do that. PHP only does what you explicitly instruct it to do.

 

You should only retrieve/show the dns records for the url if a) a url has been submitted and b) the url passes your validation.

 

But your next problem is dns_get_record() does not take a url. It requires only a hostname (the domain). If you want to get the hostname from a url you will first want to call parse_url and grab the hostname segment, using  PHP_URL_HOST  flag. Also if you want to validate a url use filter_input using  FILTER_VALIDATE_URL  as the filter

 

Pseudo code for how to layout your code

<?php

if ($_GET['url'] exists)
{
     if(url is valid)
     {
          get hostname from url
          get dns record for hostname and save to a variable
     }
     else
     {
          output error, url is not valid
     }
}

?>

show form

<?php

if (have dns records)
{
    output dns records here
}

?>

Thanks for the help but I m completely new to the programming, I filled the above based upon tutorials taken from w3schools. So the Pseudo code layout you just placed, i m unable to enter information correctly. I would be happy if you could put the code in there so i can understand based upon my above novice coding. I tried filling those places but I m unable to get anything and I m sure I am adding all of them wrong. 

Link to comment
Share on other sites

I tried my best being a complete new to programming but i m unable to get the working properly. 

<!DOCTYPE html>
<html>
<head>
<style>
table , tr, td, th {
border: 3px solid pink; 
background-color: yellow; 
border-collapse: collapse;
}

</style>
</head>

<body>
 <?php
 $site= $_GET['url'];
 if(!filter_var($site, FILTER_VALIDATE_URL) === false) {
 }else {
 $site="";
 }
 
if ($_GET['url'] exists)
{

if($_GET['url'])
     {
          $site= parse_url($_GET['url'], PHP_URL_HOST);
		  
		  foreach($site as $host){
		  $result= dns_get_record([$host], DNS_A);
		  }
          
     }
     else
     {
          $siteErr= "url is not valid";
     }
}

?>

 

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="GET">
Enter URL : <input type="text" name="url">
<span class="error"> * <?php echo $siteErr;?></span>
Submit: <input type="submit" name="submit">
</form>
<div>

<?php

if ($result)
{
    print_r($result);
}

?>
</body>
</html>
Edited by heeha
Link to comment
Share on other sites

 

I would be happy if you could put the code in there so i can understand based upon my above novice coding.

It is better if you code it as you will learn more from doing it yourself than copy and pasting someone elses code. 

 

I have simplified the pseudo code

<?php

if (isset $_GET['url'])  // check url exists - documentation http://php.net/isset
{
     if(filter_input $_GET['url'] filter PHP_URL_HOST) // check url is valid - documentation http://php.net/filter_input using PHP_URL_HOST as the filter
     {
          $hostname = parse_url $_GET['url'] component PHP_URL_HOST; // get hostname and assign to variable - documentation http://php.net/parse_url
          $dns_records = dns_get_record $hostname type DNS_A; // use dns_get_record for hostname - documentation http://php.net/dns_get_record
     }
     else
     {
          echo url is not valid // echo an error message, url is not valid
     }
}

?>

your html form here
<form ...>...</form>

<?php

if (isset $dns_records) // documentation http://php.net/isset
{
    //output dns records here
    foreach($dns_records as $record)
    {
        // output dns $record here
    }
}

I have provided 90% of the code for you. If you read the documentation I have referenced in the code you should be able to fill in the missing pieces with ease.

Link to comment
Share on other sites

Well, I m still having troubles, as I read those pages, and those to me appears to technical terms I wasn't able to understand much. I would have them if they were much simplfied way. Surely I am not looking that you full fill those missing pieces, and I m  interested in filling them myself so I guess I can keep the rest those code for sometime and in the time being i would love to clarify other things. 

 

LEt say a part of that code: 

 

<form action="<?php echo $_SERVER["PHP_SELF"];?>">

name : <input name=myname"  type="text">

<input type="submit">

</form>

 

<?php 

$host= $_GET['myname']; 

 

$host = parse($url, PHP_URL_HOST); 

 

print "The host is" .$host['host']. ".";

 

?>

 

or 

<?php 

$host= $_GET['myname'];

$host= parse_url($host);

 

print "the host is " .$host['host']. ".";

 

?>

Why doesnt this code output the host such as google.com or anyother. 

Link to comment
Share on other sites

How can we answer that question when we have absolutely no idea what, if anything, you are putting the "myname" field?

 

And what is in $url?

How does it get there?

sorry, I messed the code : 

 

<?php 

$url = $_GET['myname']; 

 

$host = parse($url, PHP_URL_HOST); 

 

print "The host is" .$host['host']. ".";

 

?>

 

let say, if i put google.com in the form and then process it. shouldn't it output the hostname?

Link to comment
Share on other sites

You are calling a function called parse(), not parse_url()

 

As the documentation for parse_url states it expects a url, not a hostname to be passed as the first argument (google.com is a hostname, http://google.com is a url).

 

When calling parse_url with the PHP_URL_HOST component, it will only return the hostname from the url, not an array. You use $host not $host['host'] when outputting the host

$url = $_GET['myname']; 
$host = parse($url, PHP_URL_HOST); 
print "The host is $host.";

If you did not specify a component, then parse_url would of returned an array,

$url = $_GET['myname']; 
$segment = parse($url); // without specifying component, returns an array
print "The host is {$segment['host']}.";
Edited by Ch0cu3r
Link to comment
Share on other sites

 In which part am i going wrong? 

Am I going ok? 

<?php

$url= $_POST['url'];

if(isset($url))  //did to check if url is there or not.
{

if(filter_input($url, PHP_URL_HOST)) //did to check if your is valid or not 

{


$host= parse_url($url, PHP_URL_HOST);
print "the host is $host";  //did to output the hostname
foreach($host as $hostname); // assigned $host as $hostname


$dnsinfo= dns_get_record($hostname, DNS_A);
print_r($dnsinfo); // to check if records are getting printed or not. 
}
}


?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="Post">
Enter URL : <input type="text" name="url">

Submit: <input type="submit" name="submit">
</form>
Edited by heeha
Link to comment
Share on other sites

$url= $_POST['url'];

if(isset($url))  //did to check if url is there or not.
{

When checking to see if a user define variable, such as $_POST exists pass it isset.

// check this post variable exists
if(isset($_POST['url']))
{

$_POST['url'] will only exist when you have submitted your form. You only want the code to get the dns record when the form has been submitted.

 

 

Next problem you are using filter_input incorrectly

if(filter_input($url, PHP_URL_HOST)) //did to check if your is valid or not
{

It requires three arguments, the first is the type of input you are applying the filter to. In your case INPUT_POST. The second value it needs is the input name, in your case url (this means we are filtering $_POST['url']) and the last value being the type of filter you are going to be using which should of been FILTER_VALIDATE_URL (sorry, I gave the incorrect filter in my pseudo code earlier). The above line should be written as

if(filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL))
{
    $url = $_POST['url']; // $_POST['url'] exists and is a valid url assign to variable.

No need for the foreach loop

foreach($host as $hostname); // assigned $host as $hostname

 parse_url($url, PHP_URL_HOST);   will only return the hostname from the url. Not an array

 

You would use $host, not $hostname here

$dnsinfo= dns_get_record($hostname, DNS_A);
Link to comment
Share on other sites

It is better if you code it as you will learn more from doing it yourself than copy and pasting someone elses code. 

 

I have simplified the pseudo code

<?php

if (isset $_GET['url'])  // check url exists - documentation http://php.net/isset
{
     if(filter_input $_GET['url'] filter PHP_URL_HOST) // check url is valid - documentation http://php.net/filter_input using PHP_URL_HOST as the filter
     {
          $hostname = parse_url $_GET['url'] component PHP_URL_HOST; // get hostname and assign to variable - documentation http://php.net/parse_url
          $dns_records = dns_get_record $hostname type DNS_A; // use dns_get_record for hostname - documentation http://php.net/dns_get_record
     }
     else
     {
          echo url is not valid // echo an error message, url is not valid
     }
}

?>

your html form here
<form ...>...</form>

<?php

if (isset $dns_records) // documentation http://php.net/isset
{
    //output dns records here
    foreach($dns_records as $record)
    {
        // output dns $record here
    }
}

I have provided 90% of the code for you. If you read the documentation I have referenced in the code you should be able to fill in the missing pieces with ease.

if (isset $dns_records) // why we we have to check isset or not here as well, don't we just call the output? We already know the url exists by checking isset($_POST['url'])) and have already know the dns is working we just didnt output it above.
{
    //output dns records here
    foreach($dns_records as $record)
    {
        // output dns $record here
    }
}


2. When I enter google.com, ( as its a hostname) not a url and form only takes url as we have told php to take url only not the hostname to process via form. 

how do i make sure that when a user enter google.com, or www.google.com in the form it will still process the information and brings the dns record. 

 

Above all, i m successfully able to output every records. Well some are missing such as reverse ip. 

Well, i also now would learn to add those information to database just to make sure my coding experience keeps on growing and would learn further more. Thanks to you for helping all that out. 


<?php
$siteErr="";
$site="";


if ($_SERVER["REQUEST_METHOD"] == "GET") {
   $site = trim_useless($_GET['url']);
  
}
 if (empty($_GET['url'])) {
     $site = "";
   } else {
     $site = trim_useless($_GET['url']);
     if (!preg_match("/\b(??:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$site)) {
       $siteErr = "Invalid URL"; 
     }
   }

function trim_useless($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

why this above code wasnt working, I know i wasn't able to tell the php to not to process the form. 

Link to comment
Share on other sites

few queries: 

 

when user pass a url http:// you tube.com it will still process it. but you (space) tube.com is not a correct url, there is a un-necessary space. 2. http://youtube.com/a comes output if user put "a" in form, I tried to put the above case to trim the specialcharacters or addtional words so that only http://youtube.com is taken in form but not http://youtube.com/a but doesnt work. 

Link to comment
Share on other sites

 

 

if (isset $dns_records) // why we we have to check isset or not here as well, don't we just call the output? We already know the url exists by checking isset($_POST['url'])) and have already know the dns is working we just didnt output it above.

No, (in that pseudo code example) $dns_records is only set when when $_POST['url'] exists and it contains a valid url. If either of those two checks fails then $dns_records wont exist and PHP would produce a undefined variable error message.

 

 

 

2. When I enter google.com, ( as its a hostname) not a url and form only takes url as we have told php to take url only not the hostname to process via form.

how do i make sure that when a user enter google.com, or www.google.com in the form it will still process the information and brings the dns record.

Before validating whether the user hast entered a url we could check to see if the url provide starts with http:// if does not we would prefix http://  converting it to a url

// check this post variable exists
if(isset($_POST['url']))
{
    if(filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL))
    {
        $url = $_POST['url'];

will become

// check this post variable exists
if (isset($_POST['url']))
{
    $url = trim($_POST['url']);

    // if $_POST['url'] does not start with http://
    if(substr($url, 0, 7) != 'http://')
    {
        // prefix with http://
        $url = 'http://' . $url;
    }

    // use filter_var to validate if $url contains a valid url
    if(filter_var($url, FILTER_VALIDATE_URL))
    {

 

when user pass a url http:// you tube.com it will still process it. but you (space) tube.com is not a correct url, there is a un-necessary space.

Shouldn't do. It will fail the url validation check

 

 

 

2. http://youtube.com/a comes output if user put "a" in form, I tried to put the above case to trim the specialcharacters or addtional words so that only http://youtube.com is taken in form but not http://youtube.com/a but doesnt work.

Should still work. The code will first validate the user entered valid url, which http://youtube.com/a is. It will then call parse_url and return the hostname segment which will be youtube.com

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.