#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    221
    Rep Power
    1

    Question Notice: Array to string conversion in /home/... on line 38


    Php Whizzes,

    The following code spits this error:

    **Notice: Array to string conversion in /home/... on line 38**

    What does it mean ?


    PHP Code:

    <?php

    /*
    ERROR HANDLING
    */
    ini_set('display_errors'1);
    ini_set('display_startup_errors'1);

    //For All Error, Warning and Notice
    error_reporting(E_ALL) OR error_reporting(-1);
    //For All Errors
    error_reporting(E_ERROR);
    //For All Warnings
    error_reporting(E_WARNING);
    //For All Notice
    error_reporting(E_NOTICE);

    error_reporting(E_ALL);

    $conn mysqli_connect("localhost""root""""id");

    if(isset(
    $_GET["url_to_proxify"]) === TRUE)
       {
            
    $url_to_proxify filter_input(INPUT_GET'url_to_proxify'FILTER_VALIDATE_URL);
            echo 
    $url_to_proxify;
            
    ?>    

            <?php
            
            $page 
    file($url_to_proxify);
            
    $domain parse_url($url_to_proxifyPHP_URL_HOST);
            echo 
    $domain;
            
            
    //eg: $pattern = array("localhost", "./", "https://", "http://");
            
    $phrase preg_replace('/src="/''src="'.$url_to_proxify$page);
            
    $phrase preg_replace('/action="/''action="proxy.php?url_to_proxify='.$domain.$url_to_proxify.'"'$page);
            echo 
    $phrase;
        }
    else
        {
            echo 
    'The "else" got triggered in the "if" condition!';
        }
    ?>

    <html>
       <body>   
          <form action = "<?php $_SERVER['PHP_SELF'?>" method = "GET">
             Url: <input type = "text" name = "url_to_proxify" />
                  <input type = "submit" />
          </form>      
       </body>
    </html>
    Error was indicating this line:

    PHP Code:

            
    echo $phrase
    Last edited by UniqueIdeaMan; July 1st, 2017 at 09:03 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,286
    Rep Power
    630
    Use the proper PHP tags for your code and tell us which is line 38. You apparently still haven't read the sticky at the top of this forum that says READ THIS BEFORE POSTING.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    221
    Rep Power
    1
    Originally Posted by gw1500se
    Use the proper PHP tags for your code and tell us which is line 38. You apparently still haven't read the sticky at the top of this forum that says READ THIS BEFORE POSTING.
    Thanks for reminding, man!
    I've now edited my op.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    It means one of your variables being echoed is an array. Which one is it?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    OK, I'm bored. I'll bite yet again.

    Here we have a fairly run-of-the-mill common error, and the task at hand is to figure out the where and why of that error, for that is the only way we can fix it and move on with our lives. Ita et nos incipere!

    Let's look at this line:

    PHP Code:
    $page file($url_to_proxify); 
    The php file function reads a file into an array, in this case the variable $page, just as it should as long as $url_to_proxify represents the path to an actual readable file (It does, doesn't it?). Here's the reference: PHP: file - Manual

    Originally Posted by A Secret Magical Source Of The Secret Magical Sauce
    file — Reads entire file into an array
    And there it is in black and white, in case you didn't believe me. One presumes that if we peeked through the key-hole, we would find that the variable $page now holds an array of lines from said file formally known as $url_to_proxify. It's just a guess, though. So far, so good?

    Hold on, what's this? Here's a nice row where you seem to be attempting to replace something:

    PHP Code:
    $phrase preg_replace('/src="/''src="'.$url_to_proxify$page); 
    Let's look a little closer at the preg_replace function. The reference is here: PHP: preg_replace - Manual

    Strings and arrays, arrays and strings! I'm so confused. I shall once again crack open the PHP "MAN" Pages...

    Originally Posted by The Genie I Keep Locked In The Basement Who Does My Bidding
    preg_replace() returns an array if the subject parameter is an array, or a string otherwise.
    This tells me that with preg_replace, you can target a string or an array, so you are OK there because $page is an array. Had it been an object, all hell would have broken loose, and the world as we know it might have very well come to an end. But it's an array, so all is cool. You are taking the array that preg_replace just performed some switcheroo with and setting $phrase equal to that little bit of tap dancing magic, so $phrase is an array as well. It's very important to note that the content of the array $page has not changed, no siree! And this little bit of code proves it:

    PHP Code:
    <?php
    $string 
    'June 15, 1985';
    $pattern '/(\w+) (\d+), (\d+)/i';
    $replacement '${1}1,$3';
    echo 
    preg_replace($pattern$replacement$string);
    echo 
    "<br>" $string;
    ?>
    Here, $string starts out as 'June 15, 1985', and ends up as 'June 15, 1985'**. Capisci?

    ** Since you ask (And you do, right?) June 15, 1985, is the day I lost my virginity. Mmmmm, memorable.

    OK, back to business. We come directly to this line. It looks familiar:

    PHP Code:
    $phrase preg_replace('/action="/''action="proxy.php?url_to_proxify='.$domain.$url_to_proxify.'"'$page); 
    The value of the array $page has not changed from when you read that big fat file into it with $page = file($url_to_proxify). It's the exact same array and nothing has changed it in any way. But now you are using preg_replace again to set $phrase to a different value. Thus, the line before this line serves absolutely no purpose what so ever.** However, again, $phrase is an array, which means thus far everything is peachy keen. Or is it hunky dory? I'm really not sure, could be both. Still with me?

    ** Why are you doing that?

    Now we get to the MEAT, it's all been building up to this!

    Are you ready? Fasten your seat belt, because this is going to BLOW YOUR MIND!

    PHP Code:
    echo $phrase
    Again, I turn to the trusty PHP "MAN" pages: PHP: echo - Manual

    And what prey tell do these illustrious pages tell me? Here it is right at the top of the reference page:

    Originally Posted by PHP "MAN" Pages Written By GOD Himself
    echo — Output one or more strings
    Now, there's some discussion as to whether or not God is a man or a woman or some other ephemeral thing, but what we do know is that there's that word. String. So the echo function only works with strings! Who knew! Well, PHP knew, because here is the answer we just arrived at right in the ever so illuminating error message:

    Originally Posted by All Knowing PHP Interpreter Who Certainly Knows Better Than You
    **Notice: Array to string conversion in /home/... on line 38**
    This means that PHP thinks $phrase is an array, and low and behold, IT IS!

    ¡Ay, caramba! Echoing an array this way will throw an error. PHP: InvalidArgumentException - Manual

    Try this:
    PHP Code:
    // var_dump($phrase) with the <pre> tags will also work.
    echo "<pre>";
    print_r($phrase);
    echo 
    "</pre>"
    This is just f**king amazing! It displays the array in an easy to read format as if by magic!

    Thank you for allowing me to expand on the limitations of the echo function. I feel as though my life is suddenly more fulfilled.

    Sit caput tardius esse dissecuerit off dant, omnique volatili bestiarum exterminati sunt,

    Mr. Arty Zifferelli, resident idiot.
    Last edited by Arty Zifferelli; July 2nd, 2017 at 10:42 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,623
    Rep Power
    1811
    You may be a total numpty, Arty, but this had me rolling with laughter!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    Originally Posted by SimonJM
    You may be a total numpty, Arty, but this had me rolling with laughter!
    Thank you.

    When I discovered that UniqueIdeaMan was using Windows to run his code, I hesitated because I am a follower of Richard Stallman.

    But I feel is is my duty, ney my calling, to impart my sage knowledge to the young naive programmer, for I was once like he.

    My guru, Sepodati, has taught me well.

    - Arty
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    221
    Rep Power
    1
    Originally Posted by Arty Zifferelli
    It means one of your variables being echoed is an array. Which one is it?
    $phrase
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    221
    Rep Power
    1
    Originally Posted by Arty Zifferelli
    OK, I'm bored. I'll bite yet again.

    Here we have a fairly run-of-the-mill common error, and the task at hand is to figure out the where and why of that error, for that is the only way we can fix it and move on with our lives. Ita et nos incipere!

    Let's look at this line:

    PHP Code:
    $page file($url_to_proxify); 
    The php file function reads a file into an array, in this case the variable $page, just as it should as long as $url_to_proxify represents the path to an actual readable file (It does, doesn't it?). Here's the reference: PHP: file - Manual



    And there it is in black and white, in case you didn't believe me. One presumes that if we peeked through the key-hole, we would find that the variable $page now holds an array of lines from said file formally known as $url_to_proxify. It's just a guess, though. So far, so good?

    Hold on, what's this? Here's a nice row where you seem to be attempting to replace something:

    PHP Code:
    $phrase preg_replace('/src="/''src="'.$url_to_proxify$page); 
    Let's look a little closer at the preg_replace function. The reference is here: PHP: preg_replace - Manual

    Strings and arrays, arrays and strings! I'm so confused. I shall once again crack open the PHP "MAN" Pages...



    This tells me that with preg_replace, you can target a string or an array, so you are OK there because $page is an array. Had it been an object, all hell would have broken loose, and the world as we know it might have very well come to an end. But it's an array, so all is cool. You are taking the array that preg_replace just performed some switcheroo with and setting $phrase equal to that little bit of tap dancing magic, so $phrase is an array as well. It's very important to note that the content of the array $page has not changed, no siree! And this little bit of code proves it:

    PHP Code:
    <?php
    $string 
    'June 15, 1985';
    $pattern '/(\w+) (\d+), (\d+)/i';
    $replacement '${1}1,$3';
    echo 
    preg_replace($pattern$replacement$string);
    echo 
    "<br>" $string;
    ?>
    Here, $string starts out as 'June 15, 1985', and ends up as 'June 15, 1985'**. Capisci?

    ** Since you ask (And you do, right?) June 15, 1985, is the day I lost my virginity. Mmmmm, memorable.

    OK, back to business. We come directly to this line. It looks familiar:

    PHP Code:
    $phrase preg_replace('/action="/''action="proxy.php?url_to_proxify='.$domain.$url_to_proxify.'"'$page); 
    The value of the array $page has not changed from when you read that big fat file into it with $page = file($url_to_proxify). It's the exact same array and nothing has changed it in any way. But now you are using preg_replace again to set $phrase to a different value. Thus, the line before this line serves absolutely no purpose what so ever.** However, again, $phrase is an array, which means thus far everything is peachy keen. Or is it hunky dory? I'm really not sure, could be both. Still with me?

    ** Why are you doing that?

    Now we get to the MEAT, it's all been building up to this!

    Are you ready? Fasten your seat belt, because this is going to BLOW YOUR MIND!

    PHP Code:
    echo $phrase
    Again, I turn to the trusty PHP "MAN" pages: PHP: echo - Manual

    And what prey tell do these illustrious pages tell me? Here it is right at the top of the reference page:



    Now, there's some discussion as to whether or not God is a man or a woman or some other ephemeral thing, but what we do know is that there's that word. String. So the echo function only works with strings! Who knew! Well, PHP knew, because here is the answer we just arrived at right in the ever so illuminating error message:



    This means that PHP thinks $phrase is an array, and low and behold, IT IS!

    ¡Ay, caramba! Echoing an array this way will throw an error. PHP: InvalidArgumentException - Manual

    Try this:
    PHP Code:
    // var_dump($phrase) with the <pre> tags will also work.
    echo "<pre>";
    print_r($phrase);
    echo 
    "</pre>"
    This is just f**king amazing! It displays the array in an easy to read format as if by magic!

    Thank you for allowing me to expand on the limitations of the echo function. I feel as though my life is suddenly more fulfilled.

    Sit caput tardius esse dissecuerit off dant, omnique volatili bestiarum exterminati sunt,

    Mr. Arty Zifferelli, resident idiot.

    Thanks for your response.
    Yes, I was told by another few days ago to not use the echo but print_r when trying to display array.
    I forgot to update this thread about it. Sorry.
    After updating my code, it came to the following:

    PHP Code:

    <?php

    /*
    ERROR HANDLING
    */
    ini_set('display_errors'1);
    ini_set('display_startup_errors'1);

    //For All Error, Warning and Notice
    error_reporting(E_ALL) OR error_reporting(-1);
    //For All Errors
    error_reporting(E_ERROR);
    //For All Warnings
    error_reporting(E_WARNING);
    //For All Notice
    error_reporting(E_NOTICE);

    error_reporting(E_ALL);

    $conn mysqli_connect("localhost""root""""id");

    if(isset(
    $_REQUEST["url_to_proxify"]) === TRUE)
       {
            
    $url_to_proxify filter_input(INPUT_GET'url_to_proxify'FILTER_VALIDATE_URL);
            echo 
    $url_to_proxify;
            
    ?>    

            <?php
            $page 
    file($url_to_proxify);
            
    $domain parse_url($url_to_proxifyPHP_URL_HOST);
                    
            
    // Replace on all loaded pages (proxified pages) all instances of the following in order to precede proxy's domain on all links present on the proxified page in order to proxify all links present on the proxified pages: "https://www.", "http://www.", "https://", "http://", "www.").
                    
            
    $pattern = array("./""https://www.""http://www.""https://""http://""www.");
            
    $replace = array("proxified_page.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page.php?url_to_proxify='.https://www..$domain.$url_to_proxify.'""proxified_page.php?url_to_proxify='http://www..$domain.$url_to_proxify.'""proxified_page.php?url_to_proxify='https://.$domain.$url_to_proxify.'""proxified_page.php?url_to_proxify='http://www..$domain.$url_to_proxify.'""proxified_page.php?url_to_proxify='www..$domain.$url_to_proxify.'");
            
    $phrase str_replace($pattern$replace$page);
            
            
    //Deal with Image Files (Eg. Google Img File)
            
    $string_replaced_data str_replace('/src="/''src="'.$domain.$url_to_proxify$phrase);
            
            
    //Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search
            
    $string_replaced_data str_replace('/action="/''action="proxified_page.php?url_to_proxify='.$domain.$url_to_proxify.'"'$phrase);
            echo 
    $string_replaced_data;
            
            
    print_r($phrase);
        }
    else
        {
            echo 
    'The "else" got triggered in the "if" condition!';
        }
    ?>

    <html>
       <body>   
          <form action = "<?php $_SERVER['PHP_SELF'?>" method = "GET">
             Url: <input type = "text" name = "url_to_proxify" />
                  <input type = "submit" />
          </form>      
       </body>
    </html>
    As you can see, that code got rid of the error.
    However, a new problem occured.

    On the text box, type: Google.
    Then, click "submit" button.
    When Google homepage loads, do a keyword search for: cars.
    You will see Google shows you a dead link. (I was running the scripts on my site).
    Eg.
    http://yourdomain.com/search?ie=ISO-...e+Search&gbv=2

    It should've shown the url like this:
    http://yourdomain.com**/proxified_pa...e+Search&gbv=2

    Why is the highlighted parts missing from the url to where you are redirected to ? Also, in that url the $domain part is not shown. This is causing the url to show a dead page.

    Here's another attempt using cURL this time but the same problem arise:

    PHP Code:

    <?php
    //STEP 1: ERROR HANDLING
    ini_set('display_errors'1);
    ini_set('display_startup_errors'1);
    //For All Error, Warning and Notice
    error_reporting(E_ALL) OR error_reporting(-1);
    //For All Errors
    error_reporting(E_ERROR);
    //For All Warnings
    error_reporting(E_WARNING);
    //For All Notice
    error_reporting(E_NOTICE);
    error_reporting(E_ALL);
    // STEP 2: Database connection
    $conn mysqli_connect("localhost""root""""id");
    if (!
    $conn) {
        
    // message to use in development to see errors
        
    die("Database error : " mysqli_error($conn));
        
    // user friendly message
    // die("Database error.");
    exit();
    }
    /* STEP 3:
    The IF gets triggered as soon as the "submit" button is clicked in the text box labeled: Url
    Following IF code deals wih GET method
    */
    if(isset($_GET["url_to_proxify"]) === TRUE)
       {
            
    $url_to_proxify filter_input(INPUT_GET'url_to_proxify'FILTER_VALIDATE_URL);
            
    $ch curl_init();
            
    curl_setopt($chCURLOPT_URL"$url_to_proxify");
            
    curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
            
    curl_setopt($chCURLOPT_HEADER5);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
            
    $curl_result curl_exec($ch);
            
    $domain parse_url($url_to_proxifyPHP_URL_HOST);
            
    //eg: $pattern = array("./", "https://www.", "http://www.", "https://", "http://", "www.");
            
    $pattern = array("./""https://www.""http://www.""https://""http://""www.");
            
    $replace = array("proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'""proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'");
            
    $string_replaced_data str_replace($pattern$replace$curl_result);
            
    //Deal with Image Files (Eg. Google Img File)
            
    $string_replaced_data str_replace('/src="/''src="'.$domain.$url_to_proxify$string_replaced_data);
            
    //Deal with all the links presented by the searchengine result pages (SERPS). Eg. Google Search
            
    $string_replaced_data str_replace('/action="/''action="proxified_page_2.php?url_to_proxify='.$domain.$url_to_proxify.'"'$string_replaced_data);
            echo 
    $string_replaced_data;
            
    curl_close($ch);        
        }
    else
        {    
            
    //Html Form
            
    ?>
            <html>
                <body>   
                    <form action = "<?php $_SERVER['PHP_SELF'?>" method = "GET">
                    Url: <input type = "text" name = "url_to_proxify" />
                    <input type = "submit" />
          </form>      
       </body>
    </html>
    <?php
        
    }
    ?>
    I'm a little sleepy now and so next time I would go through your post again while wide awake and try updating both these scripts according to your suggestions and hints.
    Once again thanks for your inputs!
    Last edited by UniqueIdeaMan; July 4th, 2017 at 08:19 PM.

IMN logo majestic logo threadwatch logo seochat tools logo