Jump to content

vinny42

Members
  • Posts

    411
  • Joined

  • Last visited

  • Days Won

    3

Posts posted by vinny42

  1. 2:Hijacking is dome by stealing the sessionid. The only think you can do to prevent that is look at the IP, which cannot be spoofed.

    The problem with the IP is that a proxy can use the same IP for several clients and clients using a phone or tablet can change IP's legally while moving around, so they would have to logon again every once in a while.

     

    3. Use SSL to make the connection unsniffable, and use session_regenerate_id() (http://php.net/manual/en/function.session-regenerate-id.php) to make the sniffed sessionid useless at the next pageview.

  2.  


    How do you do that? An outline would be very much appreciated.

     

    One way of doing it is to append each error to the same file, which will be the "most recent errors" log.

    If you write the errors like this:

     

     

    [2013-10-25 13:08:23]

    FATAL Could not connect o the database.

     

    <more details here>

    [2013-10-25 13:08:45]

    FATAL Out of memory

     

    <more details here>

    [2013-10-25 13:12:09]

    INFO Bad password

     

    User xxx tried to login with password yyy.

     

     

    Then you can parse the file and recognise every error individually.  That makes it possible to create a list of just the timestamp and the single-line description, which you can then put into an email so the recipient just gets:

     

    [2013-10-25 13:08:23] FATAL Could not connect o the database.

    [2013-10-25 13:08:45] FATAL Out of memory

    [2013-10-25 13:12:09] INFO Bad password

     

    Which is easy to interpret; if there are no FATAL lines in the digest then nothing serious is going on (you may want to add the number of FATAL erros in the subject line so the recipient can see whether poop is flying or not, without reading the email itself)

     
    The actual sending of the file can be triggered by either a cronjob if you are on linux/un*x/freebsd, or a windows-scheduler job if you are on windows.
     
    After creating and sending the digest you can rename the file to the current date and compress it, so the admin can find it again later to look at the details of the messages.
     
    Does that give you enough information to get started?
     
     

    Thanks for warning me about redirecting and e-mailing
     
    Happy to help.
  3. So your script does include something before getting to the script you posted.

     

    Do you know if your server is setup to se "auto_prepend" or something like that?

     

    You may be able to find out more about what's going on if you use debug_print_backtrace() (http://php.net/manual/en/function.debug-print-backtrace.php)

    at the top of server.php. That will print out the lines of code that were executed before the debug_print_backtrace() was called.

  4. The error message says that there is something printed before the session_start(), that's just how it is.

     

    Is this file included by another script? Do you have xdebug installed so you can trace the execution of the script to see what it is doing?

  5.  

     

    it's after a header() redirect, where you do need to use a die/exit statement because you don't want the remainder of the logic on the page to run.

     

    I missed the redirect, or I would have posted a very shouty post about that:

     

    Don't redirect when there is an internal error in the page. Google and such will follow that link and could possibly decide that your page has moved. Not only  does that remove the orignial liknk from the index, it could also result in a duplicate content penalty. One night of database problems can seriously hurt your ranking if you redirect.

     

    A more gentle approach is not to exit and not to redirect, but to throw an exception taht is caught by a piece of code that prints a userfriendly errormessage and sets the HTTP statuscode to "temporarily unavailable". that leaves your logic in tact and stops others from misinterpreting the situation.

     

    Still no die() though. :-)

  6.  


    Otherwise it's the question of the starting of the session just to have one or two variables in a slightly quicker no need to init

     

    Indeed, sessions have limited practical use and should be used with care, just like any secondary storage method.

     

     


    (but is it faster because are you saying that sessions are stored in file?. Where is it stored after that,

     

    Sessions are stored as a file on disc when when you call session_start() the file is read, unserialized, and the content is place in the superglobal $_SESSION. So there is no separate storage, it's just part of your script and when the script ends the contents of $_SESSION is written back into the file. (or any other medium if you implement a custom sessionhandler (RTM))

     

     


    So, 128 x 10,000 / 1024 = 1250 MB (is that right?) 1.2 GB

     

    Not right; 128bytes x 10.000=1.280.000 bytes, or 1.2 MB

     

     

     

    I had to recently set up a host which only had 500MB RAM and was steered away from Apache / HTTPD because of how much constant RAM it required, stating that it can easily eat 500MB and start paging out...

     

    As I recall it can take 2-4MB per process, so you should be able to handle 50 processes realtively easily.

    But a 500MB server is absolutly miniscule for today's standards.

     

     


    hence why I was looking into lighttpd and nginx, but they do have issues with PHP (they prefer static pages) and multiple ssl was out for both I believe. 

     

    nginx has no issues with php, it's not as easy to setup as for apache but it works just fine.

    SSL is available got nginx too: http://nginx.org/en/docs/http/ngx_http_ssl_module.html

     

    These last few questions fall into the "google first, post questions later" category. :-)

  7.  


    I never use die!

    +1

     

    Your code should not die() when the connection fails, because that means your website will show a blank or at least half-completed page. Not good.

     

    Also, don't send emails when errors happen, log them to a file. If this error happens on a popular website it will send a few hundred thousand emails per hour, your mail account will break.

    Mail the logfile (or better: a digest of the logfile) once every 15 minutes or so.

     

    As for handling the errors: make it throw exceptions, and let your aplication decide what to do when it catches a database-related exception. Usually you will just want tp display an HTML page telling the user that there is a problem and that you have been notified about it.

  8.  


    It's never all over!

     

    It is, when the fat lady sings! :-)

     

     

    Absolutely, but i'm wondering why run both (when using a "stay logged in" system).

     

    It's up to you :-)

    If your stay-loggedin system stores all the data you need, then you dont need sessions.

     

    But generally, a stay-loggedin system only authenticates the user and stores no other data, because it is much more convenient to use PHP's sessions for that.

     

     

    Also by storing the data in the server are you not replicating already stored data?

     

    Yes, but that by itself is not a problem. The only issue with that is that the data in the session can (and will) get outdated, so if you store accessrights in the session because "it's much faster to get them from the session" then a user can work with an old set of rights for hours; the session will not be updated untill it is destroyed and the user logs i again. (I've seen pretty weird ways to get around this, like looking up the sessionfile and modifying it... yuk). So keep important data where it belongs: in the database. Use the session for volatile data.

  9. Also note that you are using one "=" sign, so you are putting zero into $numrows, instead of checking if $numrows has zero in it.

     

    if ($numrows=0){ //nothing here, input data

     

    should be

     

    if ($numrows==0){ //nothing here, input data

     
    and to make prevent this bug in future; teach yourself to always put the constant first:
     

    if (0==$numrows)

     

    Then, if you forget an "=" sign you will get an error from PHP because you cannot change the value of the constant "0".

     

  10. It looks like you are creating a registry pattern.

     

     


    Basically I want to cut out the step of having to store the array into a temporary variable, modify it and reassigning it

     

    That would mean making the class aware of what you are storing in it, which I feel goes beyong the scope of what a superglobal of registry is supposed to do.

    I'd feel more comfortable with a "fruitlist" object that you can store in the registry, then you can modify that by getting it from the registry and calling a modifier method on it to replace the element.

  11.  


    I think this is something that may have been true in the early days of prepared statements, but is less true now that the tech has evolved. For example MySQL will use the first set of parameter values when optimizing the plan. SQL server will do that as well, in addition to storing multiple plans and re-evaluating them if the data statistics change dramatically.

     

    I know PgSQL does this, up to a point, but having combatted this in MySQL less than a year ago I didn't think they would have solved that so quickly.

     

     

    This is basically what PDO is when PDO::ATTR_EMULATE_PREPARES is set to true. Under that setup PDO will parse the query and substitute the values itself rather than using the DB's native prepared statements api/syntax. 

     

    Interesting, more ways that PDO creates inconsistend API behaviour :-)

  12. Prepared statements are very safe, but it is not a magic bullet. A prepared statement is "prepared" without any data in it, which means that the database cannot use the data to compile it's plan, which means the databae may decide that it shouldn't use an index, which can be very bad.

     

    A simple solution to this, that apparently ony I know about (given the bizar responses I've received to it) is to create your own databaseclass that just runs queries and returns the output.

    If you have that, you can create a method like "runquery($pStrQuery, $pArrParameters)" or if you use PHP's ability to have unlimited parameters: runquery($pStrQuery, $pIntParameter1, $intParameter2, ...)"

    The query can then have placeholders like $1, $2, or simply names like ':userid' and a simply loop can force all parameters to be parsed using the escape function, quote them and paste them into the query. that way you can be safe without having to resort to prepared statements.

     

    And before people start complaining about integers; most databases will happily accept quoted integers, there is no performance penalty at all.

     

     

     


    You still will want to do all your normal validations depending on the data.

     

    Where "normal validations" means "the kind of validations that your application does to be able to work with the data.

     

    There is no point to validating a date just before you put it into the database, because A) the database will refuse a bad date (unless you are using MySQL, in which case it may or may not work, depending on the IQ of the admin) and B) at that point there is nothing you can do to fix the date anyway.

  13.  


    I want to calculate how many questions I have answered correctly.

     

    It looks like you are storing the given answers in a session. Don't do that, put them in the database (after all, you want to know what people filled in, sessions are deleted after a while)

    When the give answers are in the database you can let the database do pretty much all of the work.

    Selecting the next question can be done by doing a JOIN between your questions table and user-answer table, the first question that has no answer by the current user is  the next question to ask.

     

    Counting the total correct answers can also be done by joining your questiontable (or answers table, depending on your datamodel) with the user-answers and counting the number of answers that match, like:

    SELECT COUNT(*) AS correctanswers

    FROM answers

    INNER JOIN user_answers ON answers.question_id = user_answers.question_id AND answers.thisisthecorrectanswer = true

    WHERE quiz_id = 'wwII'

    AND username = $username; // dont forget to escape the username.

  14.  


    However, like most existing engines, it has a deeper level of complication that could take me weeks, if not months, to finally sort out.

     

    Actually, the problem is quite simple if you think of these "blocks" as separate templates.  If you can render a template then it's allmost a trivial extension to find and parse a subtemplate. The only difference is that a regular template has access to the entire scope of data, and the subtemplate has to use elements of an array etc. But even that comes down to allmost exactly what it says: loop through the array and parse the subtemplate with the elementdata.

     

     

     


    However, I don't have weeks to sort through it all enough to build a guess idea of how it works (In addition, if I had that, I'd have a lot more confidence in my own ability to fix it).

     

    If you have a deadline then use an existing template system. The reason why systems like Smarty are so large is that you need those functions.

  15.  


    The problem with using IPN is that it is not real time and since he was worried about a user going back to the site before he closed his browser I recommended PDT.

     

    Most payment provides provide both methods; when a payment is completed a message is sent to the merchant and the  merchant can poll the paymentprovider to ask for the status of a payment. You should always implement both, because the call from the paymentprovider may get lost and it can take a few minutes for the payment to complete. (hours, even days for some creditcards)

     

    But this is separate from the OP's problem, which is that he want's to keep the shoppingcart in tact for one more page after placing the order. What he should do of course is move the items from the cart to the order and display the summary of the *order*, not of the cart+address.

  16. The best ways is probably a regular expression. You can remove http:// using str_replace because that can only ever appear at the start of the URL, but the 'www' must only be removed if it appears at the start of the URL and ends with a dot. Otherwise you would break domainnames like 'thisismywwwdomain.com'

     

    something like:

    $strURl = preg_replace('~^www\.~','',$strURI);

  17. Also: don't use session_destroy() when all you want to do is empty the cart. Just emty the sessionvariable that contains the cart.

     

    Why: because the session may/should contain much more than just the cart, like the user's address (so he doesn't have to type it twice if he makes two orders, or cancels his first order an re-orders immediately)

     

    Never destroy, fix!

  18.  


    I can't imagine it would be worth the effort, the code to generate the response would probably be easy to steal if it ran in the browser and it would be hard to obfuscate

     

     

    Indeed, all the code that handles the response is loaded on the browser, so the system cannot depend on anything the client does.

     

    But, don't underestimate scriptkiddies and the people with no social life, they will happily invest days to hack it, just to be able to say they did.

    If money or other prizes are involved this gets even worse.

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