Jump to content

requinix

Administrators
  • Posts

    15,227
  • Joined

  • Last visited

  • Days Won

    427

Community Answers

  1. requinix's post in Redirecting a user that is not logged in? was marked as the answer   
    soapbox

    //header('Location: /');You cannot redirect if there has been any output. Move that bit of logic to the "top" of your script.
  2. requinix's post in access.log was marked as the answer   
    No, actually a 404 would have been a good (better) thing than the 200. 404 means the server didn't know what to do with the request. As in it didn't correspond to a file or directory and it didn't have any other way to interpret what it might mean (such as through URL rewriting). A 200 means it was able to handle it in some way that seemed reasonable.
     
    Though uncommonly used, servers are supposed to accept absolute URLs in there - a place which should normally only have relative URLs. Requesting "http://whatever.your.website.is/foo" results in an HTTP request containing

    GET /foo HTTP/1.1 Host: whatever.your.website.is(plus other stuff). With our fake request, Apache reinterprets it
    HEAD http://wap.ip138.com/ HTTP/1.1 Host: whatever.your.website.isto instead mean
    HEAD / HTTP/1.1 Host: wap.ip138.comThat is, it rewrites the request URI and the Host according to the absolute URL that was used. 
    Since your server doesn't handle the "wap.ip138.com" domain, Apache picks the default virtualhost instead. It then runs the request like normal, which results in the output we saw.
     
    See also
  3. requinix's post in textarea; break on white space while entering text was marked as the answer   
    That's how they normally work...
  4. requinix's post in Equality between two object's attributes was marked as the answer   
    I too would use array_intersect_key for the property names. For the value types I'd do array_map+gettype+array_intersect_assoc.
  5. requinix's post in linearize relationship was marked as the answer   
    If you want to prefer values in master over values in admin then you need a second JOIN for that. Then you can get the correct value with a COALESCE

    COALESCE(master's value, admin's value)A more obvious solution would be to OR the franch table in
    LEFT JOIN admin as vc ON vd.mid = vc.xid OR fe.fid = vc.xidbut that only works if you know the value can't be in both admin tables (or else you'd get two matching rows). 
    But really you shouldn't have this I-don't-know-which-table-the-value-is-in issue to begin with. It suggests there are data problems.
  6. requinix's post in use of $_SESSION[] space was marked as the answer   
    It isn't that precise to begin with. Sessions are generally driven by files; when PHP loads up a session it reads the file, interprets what's inside, then restores it as $_SESSION. Then when the request is done it can/might save the session data back into that file. Naturally, adding more data means PHP will take a bit longer to read the file (start the session) and finish the request (save the session). It's all still quite fast so you don't have to worry about every single value you put in there, but you might actually notice a performance impact if you were to store, say, 1000 search results. It's not like you need those results on every page - just that there are results, then only on the search page do you need to get them all.
     

    It's all about trade-offs. By storing the query you have less in the session but loading the search page means doing the search over again. By storing the results you have all that available immediately but you do have to store it all. 
    Consider how fast the query is, how many results there are on average, how much data it takes to "remember" those results (ie, if you put them in the session), and how often the search page will be revisited.
     
    There's also another thing to consider: if you change the code in the future. If someone visits your site and gets a session, then you change your code, and they come back, they will still be using the old session. So if that code you change involves session code you need to be prepared to handle "old" sessions. It can get trickier if you stop storing some data but don't ever clean it up.
    For example, if you originally stored the search results but decided to store the query, the code would probably work fine because old users don't have a query stored, but if they have search results stored then those will stick around and get loaded/unloaded with every page until the session naturally expires.
     
    But that's applicable to anything involving sessions, not just this.
     

    Personally I would store the search parameters. Not the SELECT query itself but the information they entered in the search form. So the search text and whatever else you prompt for (eg, type of thing to search for, or places to search in). The code then takes that search information and continues from there. Alternatively you store the search query. Alternatively alternatively you'd store the search results.
     
    Whatever approach, the change to the code is about the same.
  7. requinix's post in Changing navbar if user is logged on or not was marked as the answer   
    logout stuff

    login stuff
  8. requinix's post in How do I see the source code of a web-page? - and what happened to FireBug was marked as the answer   
    I suggest trying one of these:

    It might not be 100% correct but if you can rub a few brain cells together you should be able to figure it out.
  9. requinix's post in Resize image upload to HTML page was marked as the answer   
    You can't make an image smaller and have it look exactly the same. You will lose some quality. You can try a thumbnail but you'll probably come up with a result that looks like the resized one.
  10. requinix's post in changing the database for a different country. was marked as the answer   
    Indeed. 

    That is a reasonable reason to split them: separate businesses and sites.  

    It would be and that's the problem: making the system more complex without any significant reason to do so. 

    It's simple. You don't have to think about which database you need to query if all the data is in one place. It's also good to not segregate everything based on data that can change (country), which then means you have to worry about making sure the data is stored in the right place if/when it changes.
  11. requinix's post in REST api using php - how to parse url resource id for POST was marked as the answer   
    Exactly.  

    $_REQUEST is a combination of other variables: normally $_GET and $_POST but also often $_COOKIE. They'll overwrite each other in a specific order. It's convenient to have the one variable for everything but means you can't be sure where a particular value comes from - you might think you're getting it from the URL ($_GET) but it could have been passed via a form ($_POST) or a cookie ($_COOKIE). 
    Using $_GET/POST/COOKIE specifically is considered a best practice.
  12. requinix's post in How to access a multiarray with PHP. was marked as the answer   
    echo [0]['MLSNumber']; echo [0][0]['MLSNumber']; echo [0][0]['MLSNumber'];There is no way those will work because they don't even mention $myarray. The variable has to be in there. Obviously. 

    echo $myarray['MLSNumber']; echo $myarray[0]['MLSNumber'];Better, but you're missing a very important part that can be seen quite clearly in the print_r output:
    [DATA] => Array (
    Array ( [DATA] => Array ( [0] => Array ( [CurrentPrice] => 135000.00 [MLSNumber] => 857252Look at the whole structure and read from left to right through the indentation: 
    $myarray is

    Array (an array containing
    [DATA] => Array (a "DATA" element, which is also an array containing
    [0] => Array (a 0 element, alongside a 1 and 2, which is also an array containing
    [MLSNumber] => 857252an "MLSNumber". 
    Put them all together in order and you get

    $myarray["DATA"][0]["MLSNumber"]Since the second index is a number 0-2 that means you'll need a loop to get all the elements. Like a foreach.
    foreach ($myarray["DATA"] as $data) { // $data substitutes for the [0] element echo $data["MLSNumber"];
  13. requinix's post in Mark topic as read was marked as the answer   
    It's the "Mark Community Read" link at the bottom of each (?) page.
  14. requinix's post in Couldn't Resolve Host (cURL) was marked as the answer   
    Ah, I forgot to reply after making that edit
     
    Look at what you're doing with the URLs:

    $newurl = '"'; $newurl .= $urlloop; $newurl .= "continuation="; $newurl .= $continuationToken; $newurl .= "&token="; $newurl .= $token; $newurl .= "&page="; $newurl .= $pageNumber; $newurl .= '"';There are quotes in it. Quotes. That's definitely not valid. 
    I'm also very concerned about how often you're hitting that URL: dozens of times per page. Surely you can move the cURL call out of those loops?
  15. requinix's post in Multidimensional array for nested menu items was marked as the answer   
    This sort of parent/child thing can be handled in one of two ways:
    1. Recursively. You write a function that displays the menu for a particular parent (or 0). It calls itself recursively on each child in case it has its own children.
    2. Linearly with a stack. It's basically recursive but instead of function calls you track everything in an array.
     
    #1 is definitely easier.
     
    Can you write that function? Take a look at the code in the other thread for an example. It should look something like

    function whateverYouWantToCallThisThing($link, $parent) { run a query for the children of $parent if there are children { output a <ul> for each child in the result { output the opening <li> and link whateverYouWantTocallThisThing($link, child) output the closing </li> } output a </ul> } }The output will look like
    <ul><li>Programming<ul> <li>PHP</li> <li>Python</li></ul></li> <li>Operating-Systems<ul> <li>Windows 2012 R2</li> <li>Linux Mint</li> <li>Some Stuff</li></ul></li> <li>Network</li></ul>Yes, you can format it more nicely than that - if you want to put in a bit more effort.
  16. requinix's post in echo result from prepared statement function was marked as the answer   
    Variables defined inside of a function are not available outside of a function. Return everything you need using an array.
     
    "single_row_prepared" sounds like it can handle any query, but it can't. It's only about profiles. So name it in a way that shows it gets a profile. Like "get_profile".

    function get_profile($connect, $username) { $sql = "SELECT profile_username, profile_country, profile_about, profile_age "; $sql .= "FROM profiles "; $sql .= "WHERE profile_username = ?"; $stmt = mysqli_prepare($connect, $sql); mysqli_stmt_bind_param($stmt, 's', $username); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $username, $country, $about, $age); mysqli_stmt_fetch($stmt); mysqli_stmt_close($stmt); return ["username" => $username, "country" => $country, "about" => $about, "age" => $age]; } $username = "user1"; $result = get_profile($username); echo $result["username"] . " - " . $result["country"] . " - " . $result["about"] . " - " . $result["age"];
  17. requinix's post in How to execute a mySQL subquery in php? was marked as the answer   
    I hope it's noticeably different from that because what you posted will run but certainly will not work well.
    $sql = "SELECT s.Name, sc.CID, sc.Grade FROM Student s RIGHT JOIN XS_Course sc ON s.SID=sc.SID WHERE sc.CID LIKE 'IT%' AND sc.Date>= ("select date(max(sc2.Date)) from XS_Course sc2;")";Please familiarize yourself with how strings work in PHP. 
    Same problem as the other one. Fix the other and this one will go away.
  18. requinix's post in How do I get specific values from a json encoded string? was marked as the answer   
    array_keys
  19. requinix's post in Recover DB after HDD crash was marked as the answer   
    Did you copy all the files? Each table has 2-3 files, and InnoDB has other stuff. Did you do mysqlcheck with

    --all-databases --extended --verboseCopy literally everything from the old data/ directory to the new one.
  20. requinix's post in PHP and CSS- how to style comment site was marked as the answer   
    You have a much larger problem than just the styling of the comments: people can execute PHP code on your site, let alone put HTML (and Javascript) on your site.
     
    Do you want people to write HTML? Please say no because allowing that is painful. Replace

    with
    and
    fwrite($handle,"" . $name . ":
    " . $content . "

    ");with
    fwrite($handle,"" . htmlspecialchars($name) . ":
    " . htmlspecialchars($content) . "

    ");Now go into your comments.html and make sure there isn't anything bad in it. 
    With that taken care of, find and fix the problem with your starting
    tag. I expect the browser was able to recover gracefully from it, though, as you would have noticed otherwise. 
     
    Anyways,
     
    If you need help with the appearance of your site then we need to see the HTML (your PHP isn't complicated so that's okay) and the CSS. Preferably with a link to the site, but don't provide a link until you do the comment thing I said above.
  21. requinix's post in XML Date Conversion was marked as the answer   
    How to reformat it? Parse it with either strtotime() or DateTime, then format the result with date() or ->format() respectively.
  22. requinix's post in Parse Months In cyrillic letters to numbers was marked as the answer   
    Try

    $formatter = new IntlDateFormatter("bg_BG.UTF-8", null, null, null, null, "MMM yyyy"); $dt = $formatter->parse("Май 2017"); echo date("Y-m-d", $dt);It does require you knowing what date format the filename uses, though.
  23. requinix's post in Gameserver script, Strict standards Non-Static method error was marked as the answer   
    I partially take that back: looks like the author made a mistake. Code's still old though.
     
    Go into class.core.php and make the getObject method be static.
  24. requinix's post in spaces/lines after php close ?> was marked as the answer   
    Given that you're outputting an image, it would be best not to output anything else but the image data.
     
    Best practices are to leave the final ?> out. This is a good reason why.
  25. requinix's post in Cookie errors: with error reporting it fails, without - works was marked as the answer   
    Okay, so since we've already explained the thing about if and {}s and indentation then I guess your problem is not understanding the error message? Maybe you didn't know what the isset was doing in the first place?
     

    $_COOKIE['lastVisit']If "lastVisit" is not a key in the $_COOKIE array then PHP will give you a warning. Happens any time you try to use it - except in a couple situations, like an isset(). 
    So in this code

    if (date('d-m-Y', $_COOKIE['lastVisit']) != date('d-m-Y')) {if the "lastVisit" doesn't exist then PHP will give you a warning. You are getting the warning so that means it does not exist. You have to fix your code so that you don't try to use the lastVisit value in the cookie if it does not exist. Much like in the rest of the code. 
    Most likely the problem is that you do not, in fact, understand the thing about if and {}s and indentation, because if you did then I would hope that you would realize the bug in your code and could fix it. We're talking literally adding a { and a } each in one place, but for you to do it right you have to (ahem) learn the thing about if and {}s and indentation.
×
×
  • 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.