Avatar of starhu
starhu
 asked on

How to make Php login to different sites?

Hello,

My customer wants to fill a table with site, login name, password data (many rows).

Our php should start once a day and log in to each site, retrieving some data (counting some words on pages).

How can I log in with php to another site? (knowing the user name and password)

Thank you
PHPJavaScriptAJAX

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Insoftservice

what do you mean by different sites, yahoo,gmail and so on.
Or your own websites beside this one.

for yahoo,gmail use OpenId

OpenID is a decentralized authentication protocol that makes it easy for people to sign up and access web accounts.
Yahoo, Google and facebook are all OpenID providers, so simply implementing it on your site will be enough for your users to be able to login using them (and any other OpenID provider).
Ray Paseur

How can I log in with php to another site? (knowing the user name and password)
Expect that each site will be different, so your script design will use a polymorphic adapter pattern.  

Your PHP script will make a GET request for the login page, and will retrieve the HTML.  The script will parse the HTML, isolating the form elements and filling in the name and password.  Then the script will make a POST request, returning the filled-in form.  

For this to work correctly, your script will have to act as a well-behaved web client, accepting and returning cookies, following redirection headers, etc.  It's not a task for the faint of heart, and debugging information is very hard to come by.  The first place you might want to begin is learning everything you can about HTTP protocol and the PHP cURL library.

Counting words on a web page is relatively easy.
<?php // RAY_count_words.php
error_reporting(E_ALL);
echo "<pre>";

// DEMONSTRATE HOW TO COUNT ALL THE WORDS USED ON A WEB PAGE

// USEFUL MAN PAGES:
// http://php.net/manual/en/function.file-get-contents.php
// http://php.net/manual/en/function.preg-replace.php
// http://php.net/manual/en/function.explode.php
// http://php.net/manual/en/array.sorting.php

// ACQUIRE THE DATA
$url = 'http://www.apache.org/';
$htm = file_get_contents($url);

// MUNG THE DATA INTO LOWER-CASE
$htm = strtolower($htm);

// REMOVE CSS AND JAVASCRIPT
$htm = preg_replace("/\<style.*style\>/", NULL, $htm);
$htm = preg_replace("/\<script.*script\>/", NULL, $htm);

// REMOVE THE HTML TAGS
$htm = strip_tags($htm);

// REMOVE EVERYTHING ELSE BUT LETTERS AND BLANKS
$htm = preg_replace('/[^a-z ]/', ' ', $htm);

// CONVERT ANY EXCESS WHITESPACE TO SINGLE BLANKS
$htm = trim(preg_replace('/\s\s+/', ' ', $htm));

// ACTIVATE THIS TO SEE THE "CLEAN" STRING
// echo PHP_EOL . htmlentities($htm);

// MAKE AN ARRAY OF WORDS
$arr = explode(' ', $htm);

// TURN THE ARRAY OF WORDS INTO UNIQUE KEYS, AND ZERO THE COUNTS
$unq = array_flip($arr);
foreach ($unq as $key => $nothing)
{
    $unq[$key] = 0;
}

// COUNT THE WORDS
foreach ($arr as $wrd)
{
    $unq[$wrd]++;
}

// SHOW THE WORK PRODUCTS
echo PHP_EOL . "THERE ARE " . count($unq) . " UNIQUE WORDS AMONG ". count($arr) . " TOTAL WORDS";

echo PHP_EOL . "IN ALPHABETICAL ORDER: ";
ksort($unq);
print_r($unq);

echo PHP_EOL . "IN FREQUENCY ORDER: ";
arsort($unq);
print_r($unq);

Open in new window

Best of luck with the project (it's a big one), ~Ray
starhu

ASKER
The different websites are websites of other companies.
Every website is different. The only common think is that one can enter a user name in one text field and the password in the other text field.
We don't own these web sites.

This is about completion and tender checking on  corresponding web sites.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Insoftservice

Ya, agreed every websites are different, but some have there own api to do so, which we have to integrate.
For our website we have info that what additional data has to be passed for the authentication.
You can use rest ,curl method to pass such parameters.
For example for http://lovetomarry.com beside username/password we have to send additional parameters which you would have to pass through url.

file_get_contents,curl,soap

http://www.electrictoolbox.com/php-file-get-contents-sending-username-password/
starhu

ASKER
Thank you,

We have been trying, so far no success. For example a Joomla site has many hidden parameters for example hash...
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question