Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Try out my first PHP web app!

  • This topic is empty.
Viewing 15 posts - 151 through 165 (of 211 total)
  • Author
    Posts
  • #182569
    chrisburton
    Participant

    How many times do you have session_start() throughout your code?

    #182577
    __
    Participant

    You should only be calling session_start once per script execution. If you’re calling it multiple times, each subsequent call is generating an error (if you don’t see these errors, then you have your error reporting configured wrong).

    I’m sure I’ve asked you before, but I am absentminded and cannot bring myself to look through the two dozen pages of this discussion: what version of PHP are you running?

    Since PHP 5.4, you can use session_status to check if a session has been started yet:

    if( session_status() === PHP_SESSION_NONE ){ session_start(); }
    

    Otherwise, you’ll need to check whether session_id returns an empty string.

    Another solution would be to redesign your application with a “single point of entry,” so you have fuller control over the program flow. Not that I’m saying you have to do that at this point.

    #182579
    chrisburton
    Participant

    I don’t think he was calling it where he’s receiving an error, just every time he initiates needing the data to be stored into a session, which is where I was going with your last paragraph.

    #182585
    __
    Participant

    Right, it wouldn’t be stopping the script. It’s either a Notice or a Strict Standards warning (I forget). But it’s still something that should be fixed.

    #182665
    chrisburton
    Participant

    Right, it wouldn’t be stopping the script. It’s either a Notice or a Strict Standards warning (I forget). But it’s still something that should be fixed.

    Ah. I thought it would in fact throw an error. I wonder if he’s receiving a notice in his logs at all. I certainly agree on the single-point-of-entry method if you’re referring to one session overall. In the video series, this is what the programmer is doing.

    #182680
    chrisburton
    Participant

    @drose379 Eh. Somewhat. I was periodically working on this comment system I’m building for Kirby CMS but as I learn more, I rethink how I’m applying the code. So I’ve taken a step back to focus on educating myself rather than my previous method.

    #182682
    chrisburton
    Participant

    Can you elaborate?

    #182684
    chrisburton
    Participant

    Ah, yes. I like to do it that way too but I’m also a visual learner so I have see and hear the logic and context within tutorials.

    #182686
    chrisburton
    Participant

    Well, I don’t know that much in regards to OOP with PHP. Still quite a novice but yes, this is the first.

    #182691
    __
    Participant

    Ah. I thought it would in fact throw an error.

    Notices and Strict Standards are errors. Just not fatal. PHP is designed to keep going at all costs, which is good in some sense but not in others. You should pay attention to all errors, especially during development. Think of it this way: it’s doing something it shouldn’t be, so go fix it.

    the single-point-of-entry method if you’re referring to one session overall…

    “Single Point of Entry” means that there is only one script/ function/ whatever that all requests go through.

    For example, a typical php project might have index.php, contact.php, about.php, etc.. Each is a point of entry: users visit these pages directly, and they do {whatever} without regard to the system as a whole. This design is easier to code, but creates more work keeping everything in sync, and in making sure that when scripts do interact, they do so smoothly.

    In contrast, another project might have all of those functions, but when you visit the index or contact or about url your request is actually handled by a single script (say, app.php) which does common setup work, then manages doing the requested task, then cleans up, and so forth. None of the individual “task” scripts are actually accessible individually.

    #182806
    chrisburton
    Participant

    @drose379 Can you post your forum and comments table?

    #182807
    __
    Participant

    For some reason prepared statement 2 won’t run unless stmt 1 is closed.

    One connection, two queries, that’s why. The database is waiting for you to finish with the first query, it can’t do a second query without abandoning that.

    I cannot close the statement because I have to use it to echo out the forum posts.

    You don’t have to. I recommend against generating output directly from fetching a query. If something goes wrong, it’s harder to fix. Some alternatives:

    1. Fetch the first set of results and store the info you need in a variable. Close the first query, and run the second.
    2. multi_query might be suitable for what you’re doing, if you already know everything you need to write both queries (i.e., the composition of the second query does not depend on the results of the first). So, something like this would work just fine:

    $SQL = "select this from table1 where id=7;"
        ."select that from table2 where id=7;"
    

    but something like this would not:

    $SQL = "select this from table1 where id=7;"
        ."select that from table2 where this = query1.this;"
    

    Have I ever used it? Yes. But it’s usually not necessary. And I don’t use it much anymore, in favor of stored procedures.

    #182810
    chrisburton
    Participant

    Has anyone helped you set up your database?

    #182813
    chrisburton
    Participant

    I was told to split up the comments from the actual forum posts

    Right. Totally agree with that. Just curious as to how you set it up exactly. The only difference with your forum compared to my comment system is that you created custom login script where I use Opauth for social login (Twitter, Facebook, Instagram, Google and Yahoo accounts). That way people don’t have to waste time filling out a form.

    #182817
    chrisburton
    Participant

    Use in what way? What is your output?

Viewing 15 posts - 151 through 165 (of 211 total)
  • The forum ‘Back End’ is closed to new topics and replies.