Jump to content

Automated authentication


yknot7

Recommended Posts

Hi -

 

I am trying to get content from a page using the code below

 

<?php
$url = "http://page.com/search?p_text=apache";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,'<table cellpadding="2" class="standard_table"');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
preg_match_all("|<tr(.*)</tr>|U",$table,$rows);
foreach ($rows[0] as $row){
    if ((strpos($row,'<th')===false)){  
        preg_match_all("|<td(.*)</td>|U",$row,$cells);      
        $number = strip_tags($cells[0][0]);     
        $name = strip_tags($cells[0][1]);      
        $position = strip_tags($cells[0][2]);       
        echo "{$position} - {$name} - Number {$number} <br>\n";   
    }
}

 

The issue is that to get to "http://page.com/search?p_text=apache" i need to bypass a authentication page that is dynamic based on the url in this case "apache"

 

https://login.page.com/login?v=1.2&site2pstoretoken=dynamicbasedonincomingurl&username=username&password=pass&locale=

 

I need to modify the code above to automate the login process picking up the dynamic portion of the generated login page and passing it to reach the destination page.

 

Any help appreciated.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/191914-automated-authentication/
Share on other sites

What technology is the target page created with? If it is aspx, the page works very differently to a normal HTML form and you will probably need to use cURL to manipulate the data. In fact, you will probably need to use cURL anyway, it's hard to say without seeing the page you are attempting to scrape, or at least seeing examples of the HTML on the page you need to extract the authentication from.

Here is the code for the login page

 

<form method="post" action="https://page.com/auth" name="LoginForm" autocomplete="off">
<input type="hidden" name="v" value="v1.4">
<input type="hidden" name="site2pstoretoken" value="v1.2~7658BE64~D11DD850F150B6E3971FEC75FE4FFA587D9C0A47F86156AD65C8F9F1D66E64C53F9865943B27C112476070CD50F585B58">
<input type="hidden" name="locale" value="">

<tr>
<td align=right valign="middle" class="bodycopy">Username  </td>
<td height="27" align=left valign="top" class="bodycopy">
<input type="text" name="username" size="20" maxlength="80" value=""></td>
</tr>

<tr>
<td align=right valign="middle" height="27"> <span class="bodycopy">Password  </span> </td>
<td align=left valign="top" class="bodycopy" height="27">
<input type="password" name="password" value="" size="20" maxlength="255">
<a href="javascript:doLogin(document.LoginForm);" onMouseOver="self.status='Press Go to Sign In'; return true"> <img src="go_button.gif" border=0 alt="Sign In" align="middle"> </a> </td>

</td>
</tr>
</table>
</form>

 

Thanks for the help

preg_match_all('#<input type="hidden" name="([^"]*)" value="([^"]*)"#i', $source_of_page, $matches);

echo '<pre>'
print_r($matches);

You should see all the name value pairs on the page in a multi-dimensional array, just add these pairs to your CURLOPT_POSTFIELDS in the correct manner and it should be job done.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.