The child site will need to ask the parent site the following queries:

Query 1:
SELECT advertisement_url, advertisement_image FROM advertisement WHERE advertisement_type = 'banner' ORDER BY RAND() LIMIT 0,2

Query 2:
SELECT advertisement_url, advertisement_image FROM advertisement WHERE advertisement_type = 'sidebar' ORDER BY RAND() LIMIT 0,2

Query 3:
SELECT * FROM site WHERE site_name = ? AND site_url = ? AND site_token = ? AND site_active = 1

All information is held in parent database. What I want to happen for Query 1 is to get an array of the advertisements so then I can grab them and loop through them to display the ads. For query 3 I need either a true or false value. If it's true then the site runs and if not then an error message displays.

    why not give the child site access to the parent db?

      dagon;10991136 wrote:

      why not give the child site access to the parent db?

      How do you go about giving one site access to a separate site's database?

        So it's that simple huh? I can't believe I didn't think of that. I did really want to do a REST API for learning purposes, but maybe some other project.

        So, all I need to do is create a new PDO object that connects to my parent site's DB and then it's just like querying a site's local database? Obviously I would create a new user with SELECT privileges only, right?

          RyanMinor;10991140 wrote:

          So, all I need to do is create a new PDO object that connects to my parent site's DB and then it's just like querying a site's local database?

          yup, just have to set up parent to allow access from the particular ip and particular user credentials.

            dagon;10991141 wrote:

            yup, just have to set up parent to allow access from the particular ip and particular user credentials.

            Hmm...the parent site is on a shared host. That might be a problem as I am not sure how to do this.

              I realized that allowing external access is not an option due to the volume of sites that will be running off of the parent site. I will not want to be manually allowing that many external IP connections. So, I think I will need to do the API.

              Here is my child site class that will be sending the request:

              public function siteCheck() {
              		if ($result = TRUE) {
              			return TRUE;
              		}
              		die('Your website is not valid!');
              	}
              

              Here is the parent file that will be receiving the request and returning the response:

              <?php
              // Get the URL
              $url = '';
              
              // Break it apart in paramters.
              $parameters = '';
              
              // Query the site table based on the paramteres.
              $query = $database->prepare("SELECT * FROM site WHERE site_name = ? AND site_url = ? AND site_token = ? AND site_active = 1");
              $query->execute(array($name, $url, $token));
              $result = $query->fetch(PDO::FETCH_ASSOC);
              
              if ($result[''] == 1) {
              	$response = '';	
              } else {
              	$response = '';	
              }
              
              // If one entry was found then the site is valid.
              
              // Encode the result.
              

              My question is, how do I properly code this piece of functionality? Thanks!

                Personally, I would probably just do a normal GET request from the child sites and include whatever parameters are necessary (e.g. for authentication, data parameters, etc.) in the query string. That way you'd simply extract the necessary pieces of information from the $_GET array, perform the SQL query, and echo out the response. The child sites could make the request with a simple [man]file_get_contents/man call.

                As for the format of the response, if you're just looking for a boolean true/false response, you could simply echo a '1' or a '0' and have the child sites interpret that response accordingly. If you need to return actual data (e.g. an array of data or an array of rows from the D😎, just store it all in a single array and [man]serialize/man that array as the response. The child sites would then [man]unserialize/man the data received, giving them the original array back.

                  Okay thank you very much. I will give this a shot.

                    bradgrafelman;10991154 wrote:

                    Personally, I would probably just do a normal GET request from the child sites and include whatever parameters are necessary (e.g. for authentication, data parameters, etc.) in the query string. That way you'd simply extract the necessary pieces of information from the $_GET array, perform the SQL query, and echo out the response. The child sites could make the request with a simple [man]file_get_contents/man call.

                    As for the format of the response, if you're just looking for a boolean true/false response, you could simply echo a '1' or a '0' and have the child sites interpret that response accordingly. If you need to return actual data (e.g. an array of data or an array of rows from the D😎, just store it all in a single array and [man]serialize/man that array as the response. The child sites would then [man]unserialize/man the data received, giving them the original array back.

                    This worked perfectly! Thank you very much for helping me out.

                      bradgrafelman wrote:

                      I would probably just do a normal GET request from the child sites and include whatever parameters are necessary (e.g. for authentication, data parameters, etc.) in the query string.

                      Note of course that this is the essence of how a REST request works. To make it more flexible the code for parsing the URL and identifying what is being requested would be separated from the code that actually makes the requests. Then the request interface (the former part) can be extended later without having to work around bits specific to the latter part.

                        Write a Reply...