Jump to content

roopurt18

Staff Alumni
  • Posts

    3,746
  • Joined

  • Last visited

    Never

Posts posted by roopurt18

  1. No' date=' it would not. Despite the syntax error roopurt18 pointed out you can't do a redirect and then set a session variable. Once the redirect occurs, all subsequent code is not executed.[/quote']

     

    The subsequent code will execute in this case.  All he did was call header and set the location; the subsequent call was not to exit() so the session assignment will occur.

     

    On a side note, if you order things carefully you can probably perform a session assignment and header() call in the same piece of a ternary operator.

    <?php
    echo (true ? ($_SESSION['foo'] = 'xyz' && false) || (header( 'Location: http://www.mydomain.com' ) && false) || exit() : 'false condition met');
    ?>
    

     

    However you'll probably agree that this does nothing to help with readability and maintainability of the code.

  2. If I understand, then you're trying to create a drop-down list where each item is a checkbox followed by a label.

     

    These controls are often supported in languages like Java or Microsoft's Visual XYZ.  However this is a web page that displays HTML; therefore you're limited to what you can make in HTML.

     

    However if you want to get fancy you can create your own control using a combination of HTML, JavaScript, CSS, and DOM manipulation.  It'll take many hours in and of itself and is rightfully its own project.

     

    Or you can see if such a control is implemented in one of the popular JavaScript frameworks such as YUI, jquery, mootools, dojo, etc.

     

    Once you do this, the page will only work correctly if the user has JavaScript enabled.

  3. My experience with shuffling files to remote locations is to:

    1) Split the file into smaller chunks; I typically use ~2MB

    2) For each chunk

      i) Send chunk, on failure sleep for a timeout and try again

        a) If excessive failures kill process and shoot out a notice e-mail

     

    Now if the server they're sent to is the final destination, just assemble the chunks on the remote server.

     

    However if the remote server is a holding area and the file is going to be downloaded elsewhere, leave it in pieces and essentially repeat the process you used to put the file there, except download instead of upload.

     

    Anything long-lived over a network connection has a chance to fail so its almost always best to divide and conquer.

  4. <form action="" method="post" name="myForm">
    <div>
          <input type="text" name="person[0][name]" />
          <input type="text" name="person[0][email]" />
    </div>
    <div>
          <input type="text" name="person[1][name]" />
          <input type="text" name="person[1][email]" />
    </div>
    <div>
          <input type="text" name="person[2][name]" />
          <input type="text" name="person[2][email]" />
    </div>
            <input type="submit" name="button" id="button" value="Submit" />
    </form>
    

     

    <?php
    // $_POST will then essentially look like this array:
    $arr = array(
        array( 'name' => 'Bob', 'email' => 'bob@domain.com' ),
        array( 'name' => 'Sally', 'email' => 'sally@domain.com' ),
        array( 'name' => 'Fred', 'email' => 'fred@domain.com' )
    );
    // Make your sql statement
    $insert_sql = "insert into `mytable` ( `name`, `email` ) values ";
    // Add values parts
    $parts = array();
    foreach( $arr as $person ) {
        $parts[] = sprintf( "( '%s', '%s' )", mysql_real_escape_string( $person[ 'name' ] ), mysql_real_escape_string( $person[ 'email' ] ) );
    }
    // Append the parts
    $insert_sql .= implode( ', ', $parts );
    // Run query
    $rv = mysql_query( $insert_sql );
    // ...
    ?>

  5. Well PostgreSQL is a database just like MySQL.  You create tables, store data, build indexes, and retrieve the data later.  The primary advantages of PostgreSQL, IMO, is that it hasn't recently been acquired by Oracle and is still very much supported.  MySQL isn't going anywhere soon, but it's future is a little uncertain as far as I'm concerned now that it's owned by a proprietary company that already has a proprietary database product.

     

    PostgreSQL also supports some features that I'm not sure you get in MySQL, such as a variety of a database programming languages, the ability to make your own aggregates, database schemas, advanced storage types, inherited tables, etc.

     

    The PostgreSQL community is also very active should you have support issues.

     

    I converted from MySQL to PostgreSQL for my projects at work about 2 years ago and haven't used MySQL since, nor have I wanted to.

  6. For a time sensitive application I would consider storing everything in GMT time and convert it to user's timezone upon display.

     

    (edit) Additionally if you're going to be importing datetime data from outside sources consider very carefully if you want to save those columns with timezone information.  For example I have a PostgreSQL database that a mirrors a FoxPro database.  The FoxPro database does not store timezone information but I made all of my PostgreSQL columns timestamp (0) with time zone.  Eventually this turned out to be a serious bug and it was a nightmare to correctly fix all of the imported data.

  7. In my experience with PDO/PgSql, my code ends up looking like:

     

    <?php
    class PDO_PgSql extends PDO {
        /**
        * @param string $host
        * @param string $port
        * @param string $user
        * @param string $password
        * @param [string] $dbname
        * @param [string] $port
        * @param [bool] $throw_exceptions
        * @param [bool] $ssl_mode
        * @return PDO_PgSql
        */
        public function __construct( $host, $user, $password, $dbname = null, $port = null, $throw_exceptions = true, $ssl_mode = false ) {
            //
            // Build the connection string
            if( $port === null ) {
                $port = '5432'; // Default pgsql port
            }
            $dsn = sprintf(
                'pgsql:host=%s port=%s user=%s password=%s',
                $host,
                $port,
                $user,
                $password
            );
            if( $dbname !== null ) {
                $dsn .= ' dbname=' . $dbname;
            }
            if( $ssl_mode === true ) {
                $dsn .= ' sslmode=require';
            }else{
                $dsn .= ' sslmode=disable';
            }
            //
            // Driver options
            $driver_opts = array();
            if( $throw_exceptions === true ) {
                $driver_opts[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
            }else{
                $driver_opts[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_WARNING;
            }
            //
            // Create
            parent::__construct( $dsn, null, null, $driver_opts );
            //
            // Custom PDOStatement class
            $this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array( 'PDOStatement_PgSql' ) );
            //
            // Throw exceptions.  Yes, set it in two places
            if( $throw_exceptions === true ) {
                $this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            }
        }
        
    ...
    }
    ?>

     

    You'll notice that I turn on exception handling via options in the constructor as well as explicitly calling the setAttribute() method.  It doesn't seem to hurt anything and its the only way I was able to get it to work uniformly on Windows and Linux with the versions of PHP I run.

     

    Hope that helps.

  8. Are you trying to create a PHP function that works like JavaScripts setInterval or setTimeout methods?  If so you'll have to look into shared memory and pcntl_fork().

     

    If you're trying to do this on Windows I recommend a different programming language.

  9. I use PDO and PostgreSQL on Linux servers for fairly data intensive processes and analysis and performance has always been acceptable.  The advantage to prepared statements, regardless of PDO or another extension is primarily repeated queries will perform better.  In addition, the arguments will also be correctly escaped by the database wrapper as well; this eliminates your need as the developer to insert mysql_real_escape_string() calls all over the place (at least this is true with PDO/PostgreSQL).

     

    It's not always about the performance; PDO was designed to provide a single interface between different database platforms. This may not be a concern for you, but for larger businesses or sites that use multiple platforms it's a huge bonus.

    This is only true to some extent.  Once you start using SQL syntax supported by only one flavor you still end up writing if-else or switch statements.  However at least the method names and error handling can be somewhat consistent.

     

     

  10. I can't help you much as I don't really know cURL.  Some of my PHP applications do communicate with web servers as if they were a browser though and I use the HttpRequest() object for that.  The documentation is a little buried so I'll link that to you:

    http://php.he.net/manual/en/class.httprequest.php

     

    More specifically the documentation for the send() method has some examples:

    http://php.he.net/manual/en/function.httprequest-send.php

     

    In my applications that use HttpRequest, I generally do the following:

    1) Create a single HttpRequest object

    2) Perform the initial POST to log in to a page

    3) Check the response for any cookies from the web server and add them to the HttpRequest object

     

    Now if I want to make future requests to the site, I can reuse the HttpRequest object that has the set cookies by doing the following:

    1) Change the URL on the HttpRequest object

    2) Change the method, GET or POST, as necessary

    3) Add any POST data or files to upload

    4) Call send

    5) Check the response

     

    I frequently use this method to write small export applications that upload data to web sites via HTTPS.

     

    Following is a small code snippet from my code that you can adjust to set the object up:

    <?php
    class some_app{
        public function __construct() {
                    // snippet...
    	$http = new HttpRequest( $this->_url, HTTP_METH_POST );
    	$http->addPostFields( array( 'client' => $this->_cfg['www']['client'],
    		'key' => $this->_cfg['www']['key'] ) );
    	$http->send();
    	$cookies = $http->getResponseCookies();
    	foreach( $cookies as $cookie ) {
    		$http->addCookies( $cookie->cookies );
    	}
    	$json = json_decode( $http->getResponseBody() );
    	$this->_continue = $json->Continue;
    	$this->_http = $http;
                    // snippet
        }
    
    /**
     * Clears the HTTP request for another send operation.  Call this method from
             * elsewhere in your class to communicate with the web site you've logged into.
     *
     * @param string $url
     * @param array $post
     * @param array $postfiles
     * @param bool $send
     * @return stdClass|bool
     */
    private function httpRequest( $url, $post, $postfiles = null, $send = false ) {
    	$this->_http->setUrl( $url );
    	$this->_http->setPostFields( $post );
    	$this->_http->setPostFiles( $postfiles === null ? array() : $postfiles );
    	if( $send !== true ) {
    		return false;
    	}
    	$this->_http->send();
    	return json_decode( $this->_http->getResponseBody() );
    }
    }
    ?>

     

    Now I should mention some other things:

     

    1) HttpRequest() is just a wrapper for cURL so you can accomplish all of the same with cURL.

     

    2) In my case I control both the client (the program simulating the browser) and the server (the website).  Hence I know that I don't have any special handling such as URL redirects, accepting any cookies past the initial login, or worrying about session ID changes.

     

    3) The php_http extension is required for HttpRequest() and can be difficult to locate.  I believe it is part of PECL, but I've found DLLs for Windows.

     

    I hope that helps you some.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.