SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Processing data on other servers

    Due to reasons outlined here, I'm trying to transfer data between servers without using the URL. Below are the scripts I'm using to do this. The test.php is the one run in the browser, it adds a form which when submitted sends the results to an external script on another server. The index.php file processes the data. The data.php file provides the data for use by the original server and the style.php file provides CSS data which creates coloured blocks based on hex colour codes submitted by the form.

    My intention was to grab the processed results via the readfile() function. Unfortunately the session variables I placed the resulting data into are not read by the readfile() function, presumably because the server the test.php file is on is trying to access the data and not the users browser. I was hoping javascript might have the answer, but from another topic I posted this seems to be not possible

    Do any of you know how I can get around this problem?

    Code http://localhost/test/test.php:
    <?php
    	// Start reading function
    	ob_start();
    	readfile('http://test.pixopoint.com/externalform/data.php');
    	$data = ob_get_contents();
    	ob_end_clean();
    	$data_exploded = explode("|", $data);
     
    	// Adds data to variable
    	$coloura = $data_exploded[0];
    	$colourb = $data_exploded[10];
    	$colourc = $data_exploded[2];
     
     
    	// Beginning of the HTML
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head profile="http://gmpg.org/xfn/1">
    	<title>Test</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<link rel="stylesheet" href="http://test.pixopoint.com/externalform/style.php" media="screen" />
    </head>
    <body>
     
    <div id="coloura"></div>
    <div id="colourb"></div>
    <div id="colourc"></div>
     
    <h4>Form</h4>
    <form action="http://test.pixopoint.com/externalform/index.php" method="post">
     
    Colour A: <input name="coloura" type="text" value="<?php echo $coloura; ?>" />
    Colour B: <input name="colourb" type="text" value="<?php echo $colourb; ?>" />
    Colour C: <input name="colourc" type="text" value="<?php echo $colourc; ?>" />
     
    <input type="submit" />
    </form>
     
    </body>
    </html>

    Code http://test.pixopoint.com/externalform/index.php:
    <?php
    	session_start();
    	$_SESSION['coloura'] = $_POST['coloura'];
    	$_SESSION['colourb'] = $_POST['colourb'];
    	$_SESSION['colourc'] = $_POST['colourc'];
     
    	Header( "HTTP/1.1 301 Moved Permanently" );
    	Header( "Location: http://'.$_REQUEST['return_url'].'" );
    ?>

    Code http://test.pixopoint.com/externalform/style.php:
    <?php
    	session_start();
    	header('Content-type: text/css');
    	echo '
    	div {float:left;width:100px;height:100px;border:1px solid #666;}
    	div#coloura {background:#' . $_SESSION['coloura'] .'}
    	div#colourb {background:#' . $_SESSION['colourb'] .'}
    	div#colourc {background:#' . $_SESSION['colourc'] .'}
    	';
    ?>

    Code http://test.pixopoint.com/externalform/data.php:
    <?php
    	session_start();
    	echo $_SESSION['coloura'].'|'.$_SESSION['colourb'].'|'.$_SESSION['colourc'].'|';
    ?>
    Last edited by ryanhellyer; Apr 19, 2009 at 03:11.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Where do you set the session variables? You're trying to print in the CSS file the colors they submitted with the form? If so, what's the purpose of the colors in data.php?

  3. #3
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Where do you set the session variables?
    Oops! I forgot to add the code for the index.php file to my post. It is added now.

    Quote Originally Posted by Dan Grossman View Post
    You're trying to print in the CSS file the colors they submitted with the form? If so, what's the purpose of the colors in data.php?
    The data.php file generates a list of the colours. This list is used in line four of the test.php file. It grabs the data.php page data, sticks it into a variable called $data and then explodes that into an array. The data from the array is then used to repopulate the form.




    While writing this post I thought of an alternative approach. Since I'm not modifying the data, I might be able to store it all on the server which test.php is hosted on. Originally I hadn't used that approach as I wanted a way to confirm that the data had indeed been processed, but I can do a check for that by having the other server redirect back to a URL randomly generated and sent by the test.php file. This would involve less data transfer too so the pages should load a little quicker too.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Ryan,

    Due to the way PHP sessions work, the session data is 'tied' to the users browser. So when you attempt to load the remote page with your server and not the browser, your data should be none existent.

    Maybe this earlier thread may point you in the right direction, you could quite easily add some Ajax goodness to spuce it up.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Hi Ryan,

    Due to the way PHP sessions work, the session data is 'tied' to the users browser. So when you attempt to load the remote page with your server and not the browser, your data should be none existent.

    Maybe this earlier thread may point you in the right direction, you could quite easily add some Ajax goodness to spuce it up.

    Thanks I have no idea how cURL works so I'll need to do it looks like I have plenty of reading/figuring out to do

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you explain exactly what you are trying to achieve please?

    Your request doesn't seem that 'out there' to have a fairly simple solution, maybe explaining your desired goal would be better.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to release a piece of software for download, which allows users to choose a bunch of different options. When they click submit, those options are sent through to my own server for processing. My server will process them and all the user to download CSS created during this processing (that's what the style.php file is for).

    I could store everything on my own server, but I want to make the bulk of the software free to download so that users can modify it for their own purposes. However for commercial purposes I want to keep the core code which does the processing on my own server.


    The catch here, is that the number of options in the form (test.php) will be quite large and so I can't just send the data back via the URL.


    I think I answered my own question with the following though:
    While writing this post I thought of an alternative approach. Since I'm not modifying the data, I might be able to store it all on the server which test.php is hosted on. Originally I hadn't used that approach as I wanted a way to confirm that the data had indeed been processed, but I can do a check for that by having the other server redirect back to a URL randomly generated and sent by the test.php file. This would involve less data transfer too so the pages should load a little quicker too.
    I need to sleep now, but I'll have a crack at doing that tomorrow. Hopefully it works out



    If you can think of a better way for me to achieve the same effect I'd love to hear about it though!

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah OK,

    what about something along these line, caching and your business model aside....

    PHP Code:
    <?php
    #style.php
    define('RYAN_APP_THEME_KEY''userDefinedThemeKey');
    header('text/css');
    echo 
    file_get_contents(
        
    sprintf(
            
    'http://www.yourserver.com/getCSS.php?themeKey=%s',
            
    RYAN_APP_THEME_KEY
        
    )
    );
    exit;
    ?>
    PHP Code:
    <?php
    #getCSS.php
    if(isset($_GET['themeKey']) && !empty($_GET['themeKey']))
    {
        
    $sSQL sprintf(
            
    "SELECT css FROM themes WHERE themeKey = '%s'",
            
    mysql_real_escape_string($_GET['themeKey'])
        );
        
    $rResult mysql_query($sSQL);
        if(
    mysql_num_rows($rResult) > 0)
        {
            
    $aResultSet mysql_fetch_assoc($rResult);
            echo 
    $aResultSet['css'];
            exit;
        }
        
    }
    exit;
    ?>
    Code:
    <form action="http://www.yourserver.com/themer/add.php" method="post">
        <!-- FORM FIELDS -->
    </form>
    Your form would get the required data and a unique theme name from the user and submit this to 'add.php' on your server, your server would then save this to the database. Of course this could quite easily be a local file, or as hinted at, a post-processed theme based on the submitted values.

    The user would then just add 'style.php' as the src for a style element on their page, along with defining the RYAN_APP_THEME_KEY constant inside it.

    This would require the user to edit this file each time a new theme was created though, if you had a username / password combo though, you could pass these instead and load the latest CSS created.

    style.php would obtain the data returned by getCSS.php located on your server and output it as CSS based on the theme key.

    Maybe?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    You solved another problem I didn't realise I was about to have! It hadn't occured to me that I didn't have a way to access the data from the processing server directly.

    I actually modified the method you posted above quite a lot and am storing it in flat files instead. Seems to be working well though There is a few advantages for me in doing that as the way I'll have it setup may quite a few hits to the database if I went that route, whereas presumably accessing the flat file directly will load a bit quicker.


Bookmarks

Posting Permissions

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