When I press 'View source code' of a certain web page, it's kind of like this:
<form action="/WANem/index-advanced.php" method="post">
<table border="1" width="100%">
<tr>
<td width="10%" >Delay time(ms) </td>
<td width="10%" ><input type="text" name="txtDelay1" size="7" value=1200>
</td>
<input type="submit" value="Apply settings" name="btnApply">
</table>
</form>
My question is: How can i get '1200' in the code using PHP.
I mean i just want to get a certain string in the html code of another website without having to press 'view source code' and copy that string.
Thanks for any reply.
What you're trying to do is called "web scraping".
Here's a StackOverflow question with a bunch of helpful answers:
How to implement a web scraper in PHP?
And here is a tutorial that probably explains it better than I could by typing it out here:
http://www.thefutureoftheweb.com/blog/web-scrape-with-php-tutorial
Hope it helps and good luck!
In your php file it is something as simple as this:
$value = $_POST['txtDelay1'];
Although, it looks as a really basic question. I suggest you to go through some tutorials, to get the idea on how it all works.
First on in google php form tutorial: http://www.phpf1.com/tutorial/php-form.html
EDIT:
Oh, now i see your edits. In that case, you can't skip sending a http request to get the source code, just like a browser does. Next, you have to parse the response from the server, just like a browser does as well. Ah, The response will be the "Source code" you're asking for. If you can, consider using python to this. It will be much more faster and efficient.
If PHP is a must, be aware that this task is a pain in the ass;)
You can do this with file_get_contents() and preg_match().
//$url is whatever your URL is
$url = file_get_contents($url);
preg_match('|name="txtDelay1".*?value=([\d]+)|', $html, $html);
echo $html[1];
//should print your value 1200
Check out the regex here.
However
This is only going to work as long as this code appears exactly and is not duplicated. Also, if you are scraping from another site, it could be changed by the owner, and the regex would no longer work.
Related
I've been searching for quite a while on how can you view search results based on a PHP form on a client's site pulling the data from Jamaican Yellow Pages.
I've found APIs for the Canadian one (http://www.yellowapi.com/), but nothing else.
I've tried using a URL with the search parameters to redirect to the Yellow Pages results page, but obviously with no luck, something like the following:
<form action="http://jamaicayp.com/Jamaica-Kingston/<?php $_GET['what']?>"
method="GET" class="form-input">
<input type="text" name="what" placeholder="sometext"/>
<input type="submit" name="some_name" value="Find"/>
</form>
Any help would be appreciated.
It is only possible if the Jamaican Yellow Pages allow API. Otherwise you can only link people to the website. Example:
<?php
$what = $_POST['what'];
$what = urlencode($what);
?>
<form action="http://jamaicayp.com/Jamaica-Kingston/<?php echo $what;?>" method="POST">
<input type="text" name="what" placeholder="sometext"/>
<input type="submit" name="some_name" value="Find"/>
</form>
urlencode is necessary for the search to contain spaces. Invalid link:
something.com/search/this is bad link
Good link:
something.com/search/this%20is%20good%20link
You have an error, I think, in your code:
<?php $_GET['what']?>
should be
<?php echo $_GET['what']; ?>
This may still not work, though. If it doesn't, you need to use something like cURL.
There are two issues here: can you do this, and should you do this.
Can you do this? Yes. If you can browse to that page, you can "scrape" that page. This is my solution for PHP; others exist, and you might also want to look into "scraping libraries" and frameworks.
Should you do this? At the very least you ought to contact the webmaster and query whether it's OK to display their results on your site. In a way this is worse than hotlinking, since you're displaying results while someone else does the grunt work. That's OK for some sites, not so OK for others. It is possible to recognize when your site is being scraped, and tune your results so that the one doing the scraping gets either nothing or even something nasty. So: ask.
Here I am again... I've been watching some tutorials and I'm now in good way on php programing...
What I want to know is: with a template manager like for example smarty, you can make 1 php page with the code and redirect it to the view html file... something like this on the php...
$a = new Trabalhos();
$id = new Users();
$sm->assign("id", $a->select());
if (isset($_GET['del'])) {
$a->setId($_GET['del']);
$a->delete();
}
if (isset($_POST['id'])
and isset ($_POST['trolha'])
and isset($_POST['padeiro'])
and isset($_POST['arquitecto'])
and isset($_POST['engenheiro'])
and isset($_POST['medico'])
and ! isset($_GET['edit'])) {
$a->setId($_POST['id']);
$a->setTrolha($_POST['trolha']);
$a->setPadeiro($_POST['padeiro']);
$a->setArquitecto($_POST['arquitecto']);
$a->setEngenheiro($_POST['engenheiro']);
$a->setMedico($_POST['medico']);
$a->insert();
}
$lista = $a->select();
$sm->assign("lista", $lista);
$sm->display("trabalhos.html");
Then I make the view page in trabalhos.html and I put this:
<form method="post">
| id <select name ="id">
{foreach from=$id item=d}
<option value ="{$d.id}">{$d.id}</option>
{/foreach}
</select>
| Trolha <input name ="trolha" type="text" />
| Padeiro <input name ="padeiro" type="text" />
| Arquitecto <input name ="arquitecto" type="text" />
| Engenheiro <input name ="engenheiro" type="text" />
| Medico <input name ="medico" type="text" />
<input type="submit" value="Guardar"/>
</form><br>
<table border="1">
<tr>
<td>ID</td>
<td>Trolha</td>
<td>Padeiro</td>
<td>Arquitecto</td>
<td>Engenheiro</td>
<td>Medico</td>
<td>Acoes</td>
</tr>
{foreach from = $lista item = row}
<tr>
<td>{$row.id}</td>
<td>{$row.trolha}</td>
<td>{$row.padeiro}</td>
<td>{$row.arquitecto}</td>
<td>{$row.engenheiro}</td>
<td>{$row.medico}</td>
<td>Edit | <a href="?del={$row.id}">Delete</td>
</tr>
{/foreach}
</table>
OK till there I get it and I make it work with the help of the tutorial and giving my variables
Now what I really want to know is how can I do the same without the help of external template managers, everyone will say that is easier with this and that, but I'm looking to learn php, not to run pre-made scripts, if you know what I mean
Right now I don't trust on external code... to trust it I need to understand it
So if they made it work with smarty I have to make it work by myself right?
Is there need of other languages out of php to do it? like java or something similar?
Or can I do it just with php and html coding?
If is possible to do it just with php and html coding can anyone tell me for example how will I make that column with the foreach php function work? And how do I transmit to the html the variables info?
Really sorry for that huge post, but I didn't see anything like what I'm asking in here and on google i just see people and forums talking about template managers, and that's not what I'm looking for ;)
Thank you very much for the answers last post got a positive one ;)
Trying to write your own template engine is a hard task. But you can do it in plain PHP, not need for other language.
Basically, your template engine will need 2 things in order to work:
Templates. Your trabalhos.html file is a template. It's a HTML file that contains special placeholders (like {$d.id}) that have a particular meaning for your template engine.
Variables. If you want to do something useful with your templates, you must provides variables to work with (this is what you do with an instruction like this $a->setId($_POST['id']);).
Now what you need to do is write some code that is able to parse your template files (recognize the placeholders you defined) and work with them. This is quite easy to do if you only need to replace placeholders with the value of a variable but will get tricky as soon as you start to implement features a little more complex like loops.
In addition to that, you also need to write some code that enables you to bind values to your variables as well as objects to store these.
This is a huge job but if your aim is to learn PHP, it's a really good way. Good luck!
You can use file write function.
$file = fopen("trabalhos.html","w");
fwrite($file,$lista);
fclose($file);
Im new to PHP, and by that I mean BRAND NEW. Today is the first time I've really even sat down and gave it a shot for an extensive period of time.... And the thing I'm trying to achieve is kinda silly... ive been making a little flash game website for quite some time now... Just to get my hands dirty with web design and HTML and CSS i seem to be doing fine with. The website is just a simple flash game website that I made so I could play some of my favorite flash games at school when i had nothing better to do. and I had remembered on an old website i use to go to in order to do the same thing had a "panic button" underneath every game which when clicked just took you to google... I thought it was a funny and smart idea so i wanted to improve on it, i figured i would make a little PHP script that would enable the user to change the link to whatever you want. So lets say the teacher says you need to be on, oh i dont know... SCIENCE.COM! you just copy and paste the URL into the input bar on the front page and it automatically saves it in your cookies that thats the URL you want to go to when you click the "panic" button... I guess ill post the code ive been trying to get it to work with so you can all see.
Heres the PHP:
$expire=time()+0*0*12*0;
setcookie('panic', $panic, $expire);
?>
Here is the HTML:
<form method="post">
<input type="text" name="panic" size="80" id="panic"/>
<input type="submit" value="Submit"/>
</form>
<br />
<?php
echo ('click HERE to see if it worked!');
?>
I probably dont seem too smart from this... Ive literally been going at this for hours, for some reason i just cant seem to figure it out.... And i dont know if its possible, can i have the main PHP for the cookie in a separate file from the HTML or no?...
I must also point out that out of desperation i went around editing lots of names, so i must say if anything seems extremely out of place its more then likely because i was getting frustrated and messing the the code in odd ways.
I think your problem is due to misused quotes around your PHP in the following code. Also you are using curly braces for your $_COOKIE[] selector, and trying to select the cookie by its contents, rather than its name (you are using $panic instead of 'panic'):
<?php echo ('click HERE to
see if it worked!'); ?>
Single quotes mean that you don't need to escape double quotes, and also that any PHP variables you include inside the quotes won't automatically be replaced, so these will need to be concatenated outside of the quotes.
Try replacing it for this:
<?php
echo ('click HERE to see if it worked!');
?>
It's because your html is wrong. You can't set cookies that way. PHP is run server-side, not client-side. Try this.
HTML
<form method="post">
<input type="text" name="panic" size="80" id="panic"/>
<input type="submit" value="Submit"/>
</form>
<br />
<?php
echo ('click HERE to see if it worked!');
?>
PHP
<?php
error_reporting(E_ALL ^ E_NOTICE); // prevent it from erroring if something isn't defined
if($_GET["viewcookie"]){
echo $_COOKIE["panic"];
} else if($_POST){
$panic = $_POST["panic"]
setCookie('panic', $panic, time()+(60*12)); //expires in 12 minutes
echo $_COOKIE["panic"];
}
?>
Is there a way to allow user to edit a php code securely, for example this is a basic php code to echo
Hello World! onto the page.
The idea is not to allow full coding changes just things like the array or they could edit a date in mktime things like that. I thought there maybe a way to echo form input fields into a php code which will then display the results.
How could i go about allowing a user to edit the code changing (Hello World!) to something else and then click submit to display there edit.
<?php
echo "Hello World!";
?>
or another example would be how can the user edit the words in the array
<?php
$words = array("the ", "quick ", "brown ", "fox ",
"jumped ", "over ", "the ", "lazy ", "dog ");
shuffle($words);
foreach ($words as $word) {
echo $word;
};
unset($word);
?>
I presume that i would have to create a form which gets the php code and somehow get it to display the edited results?
<form name="form" method ="get" action="a.php">
<input type="text" id="edit" name="edit" size="30" />
<input type="submit" value="Submit" >
</form>
For anyone that is viewing this and would like to know what you can create using a form and php see here Form that edits php script
What you are trying to accomplish is what variables are for.
Taking this example:
echo "Hello World!";
You could change that to
echo $_POST["data"];
and in your html
<form type='post'>
<input type='text' name='data'/>
<input type='submit'/>
</form>
See it in action
Eval should be avoided at all costs, there is a very narrow set of problems where using eval is a sane solution.
You want people to run arbitrary PHP code, but not all arbitrary PHP code. Tough thing to get right.
First off do not just eval() form data. Only bad* can come of this.
<form method="POST">
<textarea name="php"></textarea>
<button type="submit">Run</button>
</form>
<pre>
<?= eval($_POST['php']) ?>
</pre>
One option that comes to mind is to use https://github.com/nikic/PHP-Parser.
Basically, the parser does nothing more than turn some PHP code into an abstract syntax tree. ("nothing more" is kind of sarcastic here as PHP has a ... uhm, let's just say "not nice" ... grammar, which makes parsing PHP very hard.)
You can then walk the AST and remove suspect expressions, reconstitute the tree to code, and then call eval() on it.
Outside of that, configuring a sandbox environment would be critical here, as nothing is foolproof. That way, when someone inevitably bricks the box, you can recover it.
php.ini configuration changes can make for a "safer" environment to execute arbitrary code by imposing restrictions. disable_functions and disable_classes can help limit the possible abuse. Setting a low memory_limit will prevent help reduce excessive resource slurping.
* Unless this is a social experiment to see how long it takes for someone to turn your machine into pudding
in THEORY you can do something like this, but PLEASE PLEASE PLEASE don't do it because it is extremely UNSECURE
<?php
if (isset($_REQUEST['do_eval'])){
eval($_REQUEST['to_eval']);
}
?>
<form action="eval.php">
<textarea name="to_eval" rows="20" cols="80"><?php if (isset($_REQUEST['eval'])) print($_REQUEST['eval']); ?></textarea>
<br />
<input type="submit" name="do_eval" value="Submit" />
</form>
if I get you right, then eval function is what you need (http://php.net/manual/en/function.eval.php)
Evaluates the given code as PHP.
Although it is very dangerous as a user can execute a destructive code or output some private data.
<?
if(isset($_POST['submit'])
{
eval($_POST['code']);
}
else
{
?>
<form method="POST">
<textarea name="code"></textarea>
<input type="submit" value="Submit"></form>
</form>
<?
}
This sounds extremely dangerous to me; since PHP code runs on the server, you are basically letting anyone and everyone tell your server what code to run, and telling it to run harmful code would be very easy. Unfortunately, I can't think of a trivial way to sanitize this type of input.
Having said that... you can have a form that submits the user's code to a page that can write that code into a .php file on your server, then redirects to the newly created .php file. But, again, I would not advise you to do this sort of thing.
I think I understand what you're trying to accomplish. The actual task I believe will require a large amount of javascript in association with your PHP.
So, let's run it down theoretically.
Let's say this is your start code:
$array = array('one', 'two', 'three');
var_dump($array);
Ok - so now you want to define that the user can modify the array. Your HTML now looks something like that code above - all escaped of course.
However, you put form element around the escaped content, and put each array element as an input field.
So, you'll end up with something like this: (Note this is HTML not PHP)
<form action="self.php">
<div>$array = array(</div>
<span>'<input name="arrayValue[]">, +</span>
<div>);<br>var_dump($array);</div>
<input type="submit" value="Process this code">
</form>
Now, you'll need to write some javascript that watches for the class 'addAnother' to be clicked. If so, it goes up to its parent element and clones it (see - that's the span) and adds it after the parent. This way you'll have another whole line that is that span - with another input.
If you style the inputs to look nice, you can make it look like the user is typing inline.
Once the submit is pressed, the values are sent to the PHP. Then, the PHP will create a new array from all of $_POST['arrayValue'];
Your actual code will do this:
$array = $_POST['arrayValue'];
var_dump($array);
And then, you'll rerender the HTML again.
I know this is all 'theory' - there's a bit more code to actually be written.
I honestly would re-think if you really want to take on this task - this is a LOT of work to do it in an interactive, secure way. Perhaps there are other ways to accomplish your core task. Best of luck!
I used this code to print the current page:
<form>
<input type="button" value="Print" onClick="window.print();" />
</form>
but when I view it using universal document converter. It has this url in the upper right corner. How do I get rid of it?
I believe that's a browser by browser setting and can't be set through the page. I'd love it if someone could prove me wrong, though. :)
Just as theIV says it depends on the browser.
One way of doing it would be to convert it to a PDF that can then be downloaded by the person wanting to print it. Maybe a starting point is this HTML to PDF converter online - try searching for more though.