3

I am reading a source code of a page in PHP. There is an hidden input field <input type="hidden" name="session_id" value= in that page.

$url = 'URL HERE';
$needle = '<input type="hidden" name="session_id" value=';
$contents = file_get_contents($url);
if(strpos($contents, $needle)!== false) {
echo 'found';
} else {
echo 'not found';
}

I want to read that hidden field value.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190

2 Answers2

8

By far the best way to do this is with the DOM extension to PHP.

$dom = new DOMDocument;
$dom->loadHtmlFile('your URL');

$xpath = new DOMXPath($dom);

$elements = $xpath->query('//input[@name="session_id"]');
if ($elements->length) {
    echo "found: ", $elements->item(0)->getAttribute('value');
} else {
    echo "not found";
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
3

I'd look into PHP's native DOMDocument extension:

http://www.php.net/manual/en/domdocument.getelementbyid.php#example-4867

Martin Bean
  • 38,379
  • 25
  • 128
  • 201