www.webdeveloper.com
Results 1 to 5 of 5

Thread: Auto-Login

  1. #1
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88

    Auto-Login

    All,

    I am attempting to automatically log in to a website and export into a CSV file. I have provided my code below. You'll notice that the URL is an IP address, which goes to a device I am directly connected to. If I put that same IP and path in my browser, the page loads just fine. However, in this scripts I am getting a "Failed to open stream" error.

    I'd appreciate some input on how to remedy this error.

    PHP Code:
    <?php 
    error_reporting
    (E_ALL); 
    ini_set'display_errors','1');
    $page file_get_contents("http://192.168.69.154/stalist.cgi");
    echo 
    $page;
    $username '****';
    $password '****';
    $loginUrl 'http://192.168.69.154/stalist.cgi';

    //init curl
    $ch curl_init();

    //Set the URL to work with
    curl_setopt($chCURLOPT_URL$loginUrl);

    // ENABLE HTTP POST
    curl_setopt($chCURLOPT_POST1);

    //Set the post parameters
    curl_setopt($chCURLOPT_POSTFIELDS'username='.$username.'&password='.$password);

    //Handle cookies for the login
    curl_setopt($chCURLOPT_COOKIEJAR'cookie.txt');

    //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
    //not to print out the results of its query.
    //Instead, it will return the results as a string return value
    //from curl_exec() instead of the usual true/false.
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    //execute the request (the login)
    $store curl_exec($ch);


    ?>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    The address is a local (internal/network) IP address. There tends to be lots of trouble when you talk about servers (where the PHP would be installed and run from) and local IP addresses.

    Unless by chance you have PHP installed on a local webserver that is on the same router/network then you won't be able to use that IP when trying to use CURL in PHP. You would have to obtain your external IP address, and even then you would need to make sure your router is set up with the proper port forwarding to handle request from outside your network.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    I have an APACHE server running PHP on a laptop that is connected to the device. For some reason, immediately after posting this question, the script started displaying the page correctly. I then moved that same line down to line 34 in my script, and am now getting a 404 error on that line.

    PHP Code:
    <?php 
    error_reporting
    (E_ALL); 

    echo 
    $page;
    $username '**';
    $password '**';
    $loginUrl 'http://192.168.69.154/login.cgi?uri=/stalist.cgi';

    //init curl
    $ch curl_init();

    //Set the URL to work with
    curl_setopt($chCURLOPT_URL$loginUrl);

    // ENABLE HTTP POST
    curl_setopt($chCURLOPT_POST1);

    //Set the post parameters
    curl_setopt($chCURLOPT_POSTFIELDS'username='.$username.'&password='.$password);

    //Handle cookies for the login
    curl_setopt($chCURLOPT_COOKIEJAR'cookie.txt');

    //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
    //not to print out the results of its query.
    //Instead, it will return the results as a string return value
    //from curl_exec() instead of the usual true/false.
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    //execute the request (the login)
    $store curl_exec($ch);

    ini_set'display_errors','1');
    $page file_get_contents("http://192.168.69.154/stalist.cgi");
    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Should these 2 URLs be different like this?:
    PHP Code:
    $loginUrl 'http://192.168.69.154/login.cgi?uri=/stalist.cgi';

    $page file_get_contents("http://192.168.69.154/stalist.cgi"); 
    Also note the the file_get_contents() call probably will not send your cookies like the cURL call will.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    $loginUrl gives me the login page which I need to access the stalist page. However, this script doesn't log me in. Additionally, when I log in manually, I am given an error that the page can not be found on localhost. That makes sense since the page isn't on localhost, it is on the device with the IP.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles