-
Posts
4,704 -
Joined
-
Last visited
-
Days Won
179
Everything posted by kicken
-
Just use a relative path from your file to the included file for your config include. The files' relative locations to each other shouldn't be changing. require '../../config.php';
-
I personally keep my pages self-contained so I have little need for passing errors from one page to the other. In instances where I do, I just create some codes for that specific instance that only those two pages know about. Ex, from one page where a user selects a course to work on: On all the work pages (via common include) if ($Course && $Session){ if (!ValidateCourseDeveloper($Course, $Session)){ $this->Redirect('course_development.php?err=permission-denied'); } if (CourseIsLocked($Course, $Session)){ $this->Redirect('course_development.php?err=course-locked'); } } else { $this->Redirect('course_development.php?err=no-course'); } Then on the selection page: if (isset($_GET['err'])){ switch ($_GET['err']){ case 'permission-denied': $smarty->assign("msg", "You do not have access to develop the selected course."); break; case 'no-course': $smarty->assign('msg', 'You must select a session and course to develop.'); break; case 'course-locked': $smarty->assign('msg', 'This course has been locked and changes can no longer be made.'); break; } } For all the other self-contained pages I usually just have a $Errors array which I put my errors in, then display on the template. Non validation type errors (eg, sql query failures) are handled by throwing an exception which gets caught at the top-level and then displays an error page to the user.
-
Just replace time() with the variable. $offset = date('m/d/Y/H:i:s', $received+$send1['time_offset']);
-
Hence the don't change it part of my statement. Generally if your going to use codes at all, your codes will either be 1) Very generalized so they apply to several situations -or- 2) Quite specific so they only apply to one particular case. If you have a new error condition, then you create a new code. If your updating code that uses a code already, decide whether the new requirements still fit with that code. If they do, keep using it, un-change. If they don't, create a new code. Changes of the error message text associated with that code generally would be limited to re-phrasing or adding/removing additional details. Not changing the actual meaning of the code. Yes, that is the idea They have global scope within the script they are defined in. You can use them in any context. If you define them in every script (via an included file for instance) then they have "global" scope throughout your application. You wouldn't need an 'ErrorCode' column in your DB if that is what your saying. Only the ErrorID (61) and ErrorMessage ('A necessary ID value was not passed'). The codes are only for making an easier to remember reference to the ErrorID that you can use in your scripts. You could, if you desired, store the code in the DB so you can easily see by looking at the DB what code name to use in your PHP file. If you did, you could then also have your codes automatically defined so you wouldn't have to update both the DB and the codes include when adding a code. Sometimes you have to do a little extra work or "waste" a little extra space to ensure something is easy to read, understand, and maintain. Take for example this bit from the mysql source. Which is easier to understand, regard what error occured: Without their defined names: int cli_stmt_execute(MYSQL_STMT *stmt) { //... if (!stmt->bind_param_done) { set_stmt_error(stmt, 2031, unknown_sqlstate, NULL); DBUG_RETURN(1); } if (mysql->status != MYSQL_STATUS_READY || mysql->server_status & SERVER_MORE_RESULTS_EXISTS) { set_stmt_error(stmt, 2014, unknown_sqlstate, NULL); DBUG_RETURN(1); } //... if (!(param_data= my_memdup(net->buff, length, MYF(0)))) { set_stmt_error(stmt, 2008, unknown_sqlstate, NULL); DBUG_RETURN(1); } //... } With their defined names: int cli_stmt_execute(MYSQL_STMT *stmt) { //... if (!stmt->bind_param_done) { set_stmt_error(stmt, CR_PARAMS_NOT_BOUND, unknown_sqlstate, NULL); DBUG_RETURN(1); } if (mysql->status != MYSQL_STATUS_READY || mysql->server_status & SERVER_MORE_RESULTS_EXISTS) { set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate, NULL); DBUG_RETURN(1); } //... if (!(param_data= my_memdup(net->buff, length, MYF(0)))) { set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL); DBUG_RETURN(1); } //... } The second makes it clear at a glance what exactly the problem is.
-
how to update infinite amount of rows in 1 query?
kicken replied to Monkuar's topic in PHP Coding Help
More queries will be more time, always, but you probably wont notice a difference til you start doing several hundred in a loop. I had a script at one point that updated users achievement data and it was doing anywhere between 700-1500 updates in a loop. Rewriting it do a single query using a giant case statement made a noticeable increase in performance. As for setting up your loop, that is simple. Just make a standard foreach loop to go through your boxes. In that loop, create and run your query. foreach ($ibforums->input['rank'] as $rank){ $sql = 'UPDATE ....'; //run query $sql; } -
The same thing that would happen if you decided to change the number, you would have to update all the references to it. Generally, once you create it, you don't change it. You can always change the actual error message text without changing the error number/phrase. Defined constants are used like variables, but without the preceeding $. Just type the name of the constant where you want to use it. if (empty($_SESSION['articleID']) || empty($_SESSION['memberID'])){ // Key(s) Missing. $_SESSION['resultsCode'] = ERR_ID_MISSING; // Redirect to Processing Page. header("Location: " . WEB_ROOT . "process_comment.php"); // End script. exit(); } As for looking up the error code, that depends on how you store the messages. If you store them in a db, you just do a db query like when you lookup anything else. You could have a function, say getErrorMessage($code) that gets the message of the given code via whatever means are necessary for how you store them. ex: function getErrorMessage($code){ switch ($code){ case ERR_ID_MISSING: return 'A necessary ID value was not passed'; case ERR_FORM_VALUES_MISSING: return 'Some form fields were left blank'; case ERR_QUERY_FAILED: return 'A database error occured'; } } echo getErrorMessage(ERR_ID_MISSING);
-
Does socket_write() block until all data is sent?
kicken replied to DWilliams's topic in PHP Coding Help
Sockets can operate in two different modes: 1) Blocking (default) 2) Non-blocking If in blocking mode, then yes, execution will stop until all the data is sent, and all your other clients will be left waiting. If in non-blocking mode, the function will send what it can then return. However, the OS does not keep the unsent data and slowly send it as it is able. This is something you have to manage yourself. socket_write returns the number of bytes successfully sent. If that is less than what you attempted to send, you need to store the excess bytes somewhere and try again later. -
Because it makes it easier for you, or someone in the future who is maintaining your code, to know what the error is without having to track down what '62' means. It is much more obvious when you use a short phrase such as 'form_values_missing'. Using a short phrase does not necessarily prevent use from using a numbered system as well. You could have a file called say errorcodes.inc.php that you include in every script and the only thing in it is several defines: <?php define('ERR_ID_MISSING', 61); define('ERR_FORM_VALUES_MISSING', 62); define('ERR_QUERY_FAILED', 75); ...
-
Regex is generally slower than functions that just deal with simple string operations. If what you want to do can be done with a few simple string functions then you should use them rather than involve the regex monster engine.
-
$idArray = array(); $friendIdArray = array(); foreach( $ibforums->input[ 'checkbox' ] AS $key => $val ){ list( $idArray[], $friendIdArray[] ) = explode( ',', $val ); } echo implode(',', $idArray); echo '<br>'; echo implode(',', $friendIdArray);
-
Its supported, it's just not what you want. Your logic was wrong. It's still wrong too. Say you have a datatable like this: id | toid | fromid ----+------+--------- 7 | 1 | 30 8 | 30 | 1 9 | 1 | 2 10 | 2 | 1 11 | 2 | 30 12 | 30 | 2 -------------------- According to your statement you would only want to update rows 7,8,9,10 to pending=0 given your original values (updating 7,9 and their corresponding rows 8,10). The queries you execute though, will also update rows 11,12 because they match your criteria of toid IN (2,30). Not what you intended.
-
So, your id's are 7,9. Based on your image of your data those rows both have a toid of 1. Are you trying to update those rows and their corresponding rows (8,10) so pending=0 on all? I don't think you'll be able to do what you want with two IN statements. You need to either get the other two row ID's (8,10) or match on both the to and from id's
-
Maybe this is what you want, but just to make sure incase it isn't: That query would be the same as doing WHERE (id=2 or id=30) AND (toid=9 OR toid=7) When means it will match any of these row combinations: id=2, toid=9 id=2, toid=7 id=30, toid=9 id=30, toid=7 If instead you intend to match only the rows where id=2, toid=9 id=30, toid=7 then you'd need this query condition: WHERE (id=2 AND toid=9) OR (id=30 AND toid=7)
-
DOMDocument->formatOutput property
-
You'll have to go through the arrays and keep a total count of items. When your total count falls inside the visible range you output those items. You'll need a way to know when to show the table as well. Not the most elegant solution, but the following is an example: <?php $sourceArray = array( array('A', 'B', 'C', 'D', 'E', 'F', 'G'), array(3, 4, 5, 6, 7, , array('!', '@', '#', '$', '%', '^') ); $start=5; $len=10; $sourceIdx=0; $pos=0; do { $arr = $sourceArray[$sourceIdx]; $arrLen = count($arr); if ($pos+$arrLen > $start){ echo "\r\nIn array {$sourceIdx}\r\n"; for ($i=0; $len > 0 && $i<$arrLen; $i++,$pos++){ if ($pos >= $start){ echo $arr[$i]; $len--; } } } else { $pos += $arrLen; } $sourceIdx++; } while ($len > 0 && $sourceIdx < count($sourceArray)); ?>
-
Most likely cause: You do not have PHP installed and running properly. Open the page in your browser and view-source. If you can see your <?php ?> tags and the code between them, then your PHP is not setup properly and your pages are not being executed.
-
There is nothing wrong with storing an image using a number for the filename. The only difficulty would be if you look in that directory you just see a bunch of numbered files and won't know what they associate with. That is not really a big issue though. You could always use a number + the original file name. Ex: 138_myfile.jpg where 138 is an auto-inc number and myfile.jpg is the original name. It doesn't really matter what you name the image so long as your maintaining a reference by storing the name in your DB along with whatever data is associated with that image.
-
If your going to try and have that page output a zip file for download, then you have to ensure it does not output anything else. No HTML, no extra space, nothing. The only thing it can output is the zip file.
-
Keep a count of the items as you output them, and output your separator at the right time. $counter=0; while($row = $result->fetch_assoc()) { echo "<option value='{$row['name']}</option>\n"; if (++$counter == 3){ echo '<option>-------------------</option>'; } } Your error is due to you missing a ; on your connect line.
-
You probably just need to put a space between the filenames. Right now your $ZIPResult variable is going to hold all the filenames squished up together in one long string without any spaces separating them. $ZIPresult .= basename($mydir) . '/' . $filename.' ';
-
Just check for your email and password fields. Ignore the button, it's not really prudent to your script. if (isset($_POST['emailid']) && isset($_POST['pass'])){ //... }
-
Seems it's a platform thing. The code on my linux box produces a pretty spread out/random result where as when run on my windows system it is more condensed. A note on the rand() page says window's rand max is 32767, may have something to do with it. Or maybe a difference in the system implementation of rand.
-
Then your not doing what I suggest. If you did what I would suggest, the user would never even have the option to login again, as them self or as another use, without logging out first. pseudo code: <?php session_start(); if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'])){ //somebody is logged in already //redirect to the home page header('Location: index.php'); exit; } if (count($_POST) > 0){ //process submitted login form. } //show login form. ?>
-
Have your login page check if someone is already logged in. If they are, you can either: A) Log them out then show the login form plus a message saying they have been logged out -or- B) Redirect them to whatever page they would normally go to after a successful login. (probably your preferred option)
-
If you have a windows active directory domain and want to use those logins for your site, you can use ldap to perform authentication against the domain. Not sure if it is possible without a domain. If you want users to be automatically logged into your site when they login on their machine you'd have to have some kind of software running on the machine (say a browser extension) that will provide whatever details are necessary. I'm not even sure how you'd accomplish it. It'd be far easier to just create a 'remember me' option and have your users check that. You'll have to clarify what it is you want to accomplish.