Jump to content

dpacmittal

Members
  • Posts

    220
  • Joined

  • Last visited

    Never

Posts posted by dpacmittal

  1. I'm wanting to change hosting provide but I dont know how to change it without any downtime. Any help is appreciated. Also, since I'll change hosts, I need to point my domain names to the new nameservers. So, do I have to edit the MX entries to point to Google Apps again or would it be retained?

     

    Just FYI, I'm switching from Linode to Web Faction.

  2. I made a CNAME static.mysite.com and pointed it to www.mysite.com. This is basically to create a cookieless domain. Now I don't want anything other than javascript, css and pictures to be served from static.mysite.com. I almost wasted one hour figuring out what htaccess would suffice this. I've got the logic but I am not able to implement it.

     

    Basically, if the HOST header has static in it, then allow only above-specified filetypes and give a 403 on the other pages.

    If the HOST header doesn't contain 'static' in it, then its normal domain and should allow access to everything.

     

    I hope I am clear.

    Can you guys help me with this?

  3. Gave a quick look and I have just one suggestion:

    Consider making a db class just for CRUD operations and another class which would handle the queries related to login. This way db class becomes reusable.

  4. Hello All,

     

    env: php5 Linux

     

    I am concerned about XSS and co related vulnerabilites. I am using a alphanumberic "white list" technique. Is the form sufficient and are there any additional concerns/gotchas?

     

    The Code:

    -----------------------------

    <?php
    function clean($input) {
    return preg_replace('/[^a-zA-Z0-9\s]/', '', $input);    //only allows letters, numbers and spaces
    }
    ?>
    
    <!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>
    <title>Safe Form</title>
    </head>
    
    <body>
    <form action="" method="POST">
    <input name="username" type="text" id="username">
    <input name="password" type="password" id="password">
    <input type="submit" name="Submit" value="Submit">
    <input name="reset" type="reset" id="reset" value="Reset">
    </form>
    
    <?php
    echobr(clean($_POST['username']));
    echobr(clean($_POST['password']));
    ?>
    </body>
    
    </html>

    I guess this would be quite enough.

  5. That would break encapsulation. I don't think this overhead would be all that significant.

    Yeah, I hope so.

     

    Well, the more you abstract things, the more performance you sacrifice. That's just the way things work. If performance is the single-most important thing in your application then write it in assembly.

    I'd rather sacrifice performance. :)

     

    Thanks for all the replies. I'll mark this solved.

  6. Thanks for the code.

     

    I too had thought of doing this way but I wanted to have better performance. In this way, you notify all the attached observers which then process through a series of conditions. This creates unnecessary overhead. Is there any way we can notify only the objects which are related to a particular state?

     

  7. PHP execute server side so no, your solution need to execute on the client.

    I think he means a web-app which can update on a single click or maybe auto-update using cron.

     

    @OP:

    You can create a php file on central server which simply echoes (or displays xml) latest version of software available.

    You can then use curl to download the archive on to client server, use zip libraries to extract the files and redirect the client-browser to upgrade.php or something.

  8. I don't know whether I'll get better answer posting this in 'Php coding' or 'Application design', so I'll just post it in 'PHP coding' since it has more traffic.

     

    Okay, so I've read about observer pattern, seen examples. Seen how to use SPL for the purpose. However, all the examples were basic and only dealt with a single hook. However, I'd like to have multiple hooks within a class and I am confused about whats the best way to achieve that.

    I've written a code last night which does this but I'm afraid its very noobish. I'd be grateful to anyone who could point me in the right direction.

     

    Code I wrote last night:

    <?php
    class Login implements SplSubject {
        private $storage;
        var $currhook;
        function __construct() {
            $this->storage = array();
        }
    
        function attach( SplObserver $observer ) {
            if(!isset($this->storage[$observer->hookName]))
                $this->storage[$observer->hookName] = new SplObjectStorage();
            $this->storage[$observer->hookName]->attach($observer);
        }
        function detach( SplObserver $observer) {
            $this->storage[$observer->hookName]->detach($observer);
        }
        function notify() {
            foreach ( $this->storage[$this->currhook] as $obs ) {
                $obs->update( $this );
            }
        }
        function log_in(){
            $this->currhook = 'beforelogin';
            $this->notify();
            echo "LOGGED IN";
            $this->currhook = 'afterlogin';
            $this->notify();
        }
    }
    class LoginObserver implements SplObserver {
        var $hookName;
        function update(SplSubject $login){
            $this->doupdate($login);
        }
        
    }
    class afterLogin extends LoginObserver {
        function __construct(Login $login){
            $this->hookName = 'beforelogin';
            $login->attach($this);
        }
        function doupdate($login){
            echo "beforelogin<br/>";
        }
    }
    class beforeLogin extends LoginObserver {
        function __construct(Login $login){
            $this->hookName = 'afterlogin';
            $login->attach($this);
        }
        function doupdate($login){
            echo "<br/>After login";
        }
    }
    $login = new Login();
    new beforeLogin($login);
    new afterLogin($login);
    $login->log_in();
    ?>
    

     

    Pardon the naming inconsistencies.

  9. how do you compute an equation inside a variable?

     

    $row['equation'] - from database. example content: ORIGINAL_PRICE - (ORIGINAL_PRICE * 0.50)

     

    example

    define('ORIGINAL_PRICE', 49.97);
    $value = $row['equation'];
    

     

    is it possible?

    This is what you need:

    http://php.net/manual/en/function.eval.php

     

    Store the equation in database as: $ORIGINAL_PRICE - ($ORIGINAL_PRICE * 0.50)

    After that, you can do:

    $value = eval($row['equation']);
    

  10. Actually you may want to use:

     

    /^(\d[ -]?){7,12}[^ -]$/

     

    This will prevent the last digit being a space or hyphen.

    Thanks a lot. I was just over-complicating stuff. It was straighforward.

    Thanks, again.

  11. UPDATE:

    I tried this new regex. It seems to be working fine except that it is accepting phone numbers with lesser digits. Here is it:

    if(preg_match('!^\d(\d|(?<=\d)(\s|\-)?){5,10}\d$!',$number))
    
    

  12. First of all, sorry for not being able to provide a more appropriate title.

     

    Write a function declared as function ReformatPhoneNumber($number), whose argument will contain string data representing some phone number data (entered by the user). A valid phone number may consist of between 7 and 12 digits (0..9). Assume that in between some adjacent digits there may optionally appear either a single space, or a single hyphen (-). Any other phone number should be considered invalid.

     

    If the phone number is valid, the return value of your function should contain a string containing between 7 and 12 digits, representing the same phone number after removing all hyphens and spaces. If the phone number is invalid, throw a standard PHP5 Exception initialized with the text "Invalid phone number".

     

    The first and the last character of the string should be a number.

     

    For example, after calling ReformatPhoneNumber('012-345 69') the return value should be '01234569'. Calling the function with any of these values: '012345', '-012345 678', '01203- 34566', '123456678875432', '1234x567' should result in an exception.

    This is a question from elance coding test. I completed it last time by using all sorts of functions and a very basic regex. This time I tried to become brave and thought of solving it completely using Regexes. However I got stuck at multiple points (hence, the inappropriate title).

     

    I tried these which are obviously wrong:

    if(preg_match('!^\d([\d]+(\s|\-)?){5,10}\d$!',$number))
    

    I know it would accept any length of string.

    if(preg_match('!^\d(\d|\s|\-){5,10}\d$!',$number))
    

    This would allow multiple spaces and hyphens next to each other.

     

    Normally, my knowledge on regex usually gets me through my work and I don't usually need help in this regard. However, this one completely got me.

    Any help would be appreciated.

     

    Thanks!

  13. My client has a video site and he wishes to display lyrics when someone watches a music video. He bought a database for that. Now, most videos' titles contain artist name and song title. I take it as argument and search it in database and present the lyrics. It works for 60% of the songs and doesn't works for the others.

     

    For eg; I searched 'Beatles let it be' and it returned wrong lyrics.

     

    My query is:

    SELECT *, MATCH(title,artist,album) AGAINST ('%$query%') as Relevance FROM lyrics WHERE MATCH(title,artist,album) AGAINST ('%$query%' IN BOOLEAN MODE) HAVING Relevance > 6 ORDER BY Relevance DESC limit 1
    

    $query is the video title.

     

    PS: This is the first time I am working with Fulltext so my query might be noobish.

     

    EDIT: I just noticed that I am using album in the query too, is it possible thats what causing the inaccuracy?

  14. You might want to look into the Observer pattern. There's plenty of examples around but this one was the first simple one I found googling 'php obeserver'

     

    http://devzone.zend.com/article/4284

    Thanks, it gave me the idea.

     

    My own plugin implementation is very similar to what you have described in your original post, and is also an implementation of the observer pattern as thorpe referenced.

     

    The concept is simple, although the terminology I use is slightly different.  There are two primary pieces, 1. events and 2. listeners that attach to events.

    Each "plugin" can be extremely simple or extremely complex, but either way it adds at least one listener to an event. (e.g. addListener('page.init', 'functionToRunOnPageInit');)

     

    Handling the listeners attached to events can be done two ways.

    1. Notify listeners that an event has occurred.  This simply runs the listeners attached to an event. notifyListeners('page.init');

    2. Let the listeners filter or change a value.  The order in which the value is modified is determined by the order in which listeners were added to the event. $aValueThatWillBeChanged = runFilters('page.init', $aValueThatWillBeChanged);

    Thanks, it helped quite a lot.

     

  15. I want to create a plugin system similar to one implemented in wordpress. Google hasn't been of any help. I tried reading wordpress source but it would take much time to understand the underlying concept. I've checked its database and it doesn't store any plugin information in the database which means that it parses all the plugins everytime the page is loaded which, in my opinion, is not (?) a good way to do it.

     

    The system they've used is the hook system. There are hooks in most of the places. You add some action to the hook and when the hook is called, the action to attached is also executed. For eg; if you attach an action of cleaning a certain folder on the hook of add-post (say), everytime you add a post, the folder will be cleared.

     

    Okay, so I've not been able to understand how wordpress does it. However, I myself have a basic idea for this. I just want opinions if it is a good way to do it. I've thought of something like this:

     

    Plugins would have to call a function to attach an action to hook eg;

    attach_action ($hookname, $callback)

     

    When the plugin is installed, attach_action would add this information to the database.

     

    When the hook is called, it fetches all the actions attached to it from the database and executes them.

     

    When the plugin is uninstalled, the record from database is removed.

     

     

    So this is it. I want to know if this is better/worse way than how wordpress does? Thanks

  16. I want to create a plugin system similar to one implemented in wordpress. Google hasn't been of any help. I tried reading wordpress source but it would take much time to understand the underlying concept. I've checked its database and it doesn't store any plugin information in the database which means that it parses all the plugins everytime the page is loaded which, in my opinion, is not (?) a good way to do it.

     

    The system they've used is the hook system. There are hooks in most of the places. You add some action to the hook and when the hook is called, the action to attached is also executed. For eg; if you attach an action of cleaning a certain folder on the hook of add-post (say), everytime you add a post, the folder will be cleared.

     

    Okay, so I've not been able to understand how wordpress does it. However, I myself have a basic idea for this. I just want opinions if it is a good way to do it. I've thought of something like this:

     

    Plugins would have to call a function to attach an action to hook eg;

    attach_action ($hookname, $callback)

     

    When the plugin is installed, attach_action would add this information to the database.

     

    When the hook is called, it fetches all the actions attached to it from the database and executes them.

     

    When the plugin is uninstalled, the record from database is removed.

     

     

    So this is it. I want to know if this is better/worse way than how wordpress does? Thanks

×
×
  • 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.