DavidAM
Staff Alumni-
Posts
1,984 -
Joined
-
Days Won
10
Everything posted by DavidAM
-
Simple Encryption Routine VBA to PHP URL Encoding Problem
DavidAM replied to Shinzan's topic in PHP Coding Help
1) Use POST rather than GET. It is hidden better (though still not invisible); the length limits are higher (you are sending a lot of data for a GET); it might avoid the "truncation" 2) urlencode()/urldecode() are different from rawurlencode()/rawurlencode(). Since you are having to urlencode it back and then rawurldecode it, there is an apparent mis-match between the VBA and PHP. There may also be some minor discrepancies in the interpretation of the specs between VBA and PHP. You can get the un-urldecoded (that is, the raw) values from $_SERVER['QUERY_STRING'], I believe. Of course, you will have to split the string up yourself if there is more than one parameter. If at all possible, I would use POST for this.- 11 replies
-
- php
- encryption
-
(and 3 more)
Tagged with:
-
A twister - Unable to prevent a second login by the same member.
DavidAM replied to ajoo's topic in PHP Coding Help
I usually just change the header() to die() until I figure out the problem. At any rate: There is no relationship between the two sessions. They are distinct client sessions (only related to the page request), one session cannot know about the other. The solution would require on login, write the session ID to the user's database record. on page access, if the session ID of the request does not match the one in the database (for the user in the $_SESSION array), tell them they logged in from somewhere else, and kick them to the logout script. That's off the top of my head, I've never tried this. -
UTF8 lithuanian characters unrecognized in MySQL database
DavidAM replied to Baltas's topic in MySQL Help
Also, make sure the HTML with the form specifies UTF-8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> -
Function no longer working / wont check if email already exists
DavidAM replied to justin7410's topic in PHP Coding Help
I see you edited your post while I was responding. If the COUNT is zero, then there are no records. See the red statements below: It is kinda hard to understand the problem, you need to be more exact in your statements: The query will never return a boolean false. Are you talking about the function or the call to mysql_query(). If the call to mysql_query() returns false, then the query failed. The call to mysql_error should tell you exactly why it failed. If the query looks correct, run it in your SQL control panel (phpmyadmin or whatever your host provides). There could be a number of problems: column or table name is wrong; database user does not have SELECT rights on the table/column; the email column width is too small and does not actually contain the full email address; the email address in the database has spaces in front or escaping characters inside, table/column is case-sensitive; etc. If you get the correct data from the SQL control panel, then the problem is in the PHP (or the database credentials). Try this in the SQL control panel: SELECT CONCAT('|', email, '|') AS emailD FROM users WHERE email like '%justin%' or this SELECT CONCAT('|', email, '|') AS emailD FROM users and have a look at your data. I don't know how I missed this before, but this statement does NOT need the parenthesis. I don't think it is causing a problem, but you should take them out: $query = " SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$username' "; -
I would probably use a different class. $(document).ready(function() { $(".myClass").click(function(e) { e.preventDefault(); loadToDiv($(this).attr("href"), '#myDivID'); return false; }); $(".myOtherClass").click(function(e) { e.preventDefault(); loadToDiv($(this).attr("href"), '#myOtherDivID'); return false; }); }); function loadToDiv(psUrl, psDivSelect) { $.get(psUrl, function(data) { $(psDivSelect).html(data); } }
-
Function no longer working / wont check if email already exists
DavidAM replied to justin7410's topic in PHP Coding Help
Debugging 101 - PRINT Add a bunch of print() (or echo()) statements at key points (just before an IF test, etc.) So you can see if the variables contain what you would expect. You definitely do NOT want to reduce the function down to a single line of code if you are are not sure it is working! Going back to your first post. I added a few print statements so we can see what is happening. <?php function email_exists($email) { $username = sanitize($email); $query = (" SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$username' "); ## Oops, you were using $email which is the UN-sanitized version of that value # Let's see what the query looks like printf("Query: %s<BR>", $query); $results = mysql_query($query); // Let's see if the query succeeded if (!$results) { printf("Query Failed!<BR>%s<BR>", mysql_error()); /* We probably should do something more intelligent than just return false, but I don't know the application so, hey, I didn't find the email address. */ return false; } // OK, The query executed, let's see what we found if (mysql_num_rows($results) == 0) { /* This should never happen. Since we used COUNT(user_id), we should at least get a row with a count of zero. */ printf("Query Returns Zero Rows???<BR>"); /* We probably should do something more intelligent than just return false, but I don't know the application so, hey, I didn't find the email address. */ return false; } // OK, Now let's see what we got $theCount = mysql_result($results, 0); # Show the count, just so we know printf("Query Says Count is %d<BR>", $theCount); return ($theCount > 0); } If you have a redirect ("header(Location: ...") after you call this function, you may need to change it to a die() statement while you debug so you can see these printed messages. -
Function no longer working / wont check if email already exists
DavidAM replied to justin7410's topic in PHP Coding Help
Did you change your check in the main file (the first line of code above).Returning -1 or -2 from this function will NOT be interpreted as FALSE (not even with two-equal signs). Be aware, a SELECT COUNT(... will always return a row. Your query will always return 1 row, unless it fails. The value of the count column may be zero, but there will be a row. -
The ID needs to be unique across ALL elements in an HTML document. Instead of using id="myLinkID" in the anchor, use a class: class="myLinkClass". Then change the selector in your function to select by class instead of ID: $(".myLinkCLass").click(function(e). I think that will fix the problem.
-
Can anybody review my code and see what im doing wrong
DavidAM replied to jasonsuthers's topic in PHP Coding Help
Please use code tags when posting source code; it will be much easier to read It appears to me that you are confusing the AM/PM hours. In the first line, $h can NOT be GREATER THAN 11 AND LESS THAN 3 -- perhaps that should be LESS THAN 15? I can't really tell what you are trying to do with the third line, the section comment says MONDAY, but it looks like you are testing TUESDAY ($d == 2). Personally, I would set the default image to closed.gif, and create an array of Open Hours. Then check the array to see if we should switch to the open.gif image. This way, if the hours change, you only have to change the DATA (in the array) instead of having to re-write a bunch of IF statements. Something like: <?php /* Array of hours we are open: The key is the Day of week (date('w')) The values are arrays - first entry is Open time, second entry is Close time - 24-hour format (date('G')) */ $openHrs = array([1] => array(11, 15), // Monday 11 AM - 3 PM [2] => array(11, 15), [3] => array(11, 15), [4] => array(11, 15), [5] => array(11, 15), [6] => array(9, 14), // Saturday 9 AM - 2 PM [7] => array(12, 16), // Sunday Noon - 4 PM ); $h = date('G'); //set variable $h to the hour of the day $d = date('w'); //set variable $d to the day of the week. $image = 'closed.gif'; if (isset($openHrs[$d])) { if ( ($h >= $openHrs[$d][0]) and ($h < $openHrs[$d][1]) ) $image = 'open.gif'; } I changed up the hours a bit as an example. Obviously if you have any split-days -- 11AM to 3PM then 6PM to 10PM -- you will have to re-work it a bit. Disclaimer: This code is un-tested. Also, I don't write code like that, it was indented and pretty when I pasted it, this @#$%&! editor keeps screwing with my code. -
Sending HTML email with multiple attachments.
DavidAM replied to njdubois's topic in PHP Coding Help
If you are wanting to send a multipart-mixed email where the email message is in HTML, and you have attachments; and there is no alternative to the HTML part, then Alkimuz was close. Leave the HEADERS as you had them in the first post, but use the HTML content type in the body: Here you are saying that $email_message is plain text. Change that Content-Type to "text/html". Note: This will NOT magically change $email_message to an HTML formatted message. That variable needs to hold an HTML formatted text string. -
I certainly was not offended. I apologize if I came off that way. Building emails from scratch can be difficult, and the structure has to be just right. This is why (I expect) so many people recommend using a third-party library. I never looked at any third-party library, I just followed the specs, and methodically built the email. Then made it a function, actually a class now, so I would not have to try to figure it out the next time. It takes time and attention to detail. I like your latest code, it is much easier to see each "header" and know what is going where. Unfortunately, it appears that I misled you when I described the MIME parts as having a "header" and "body". It might be easier to understand if I provide an example. When I am testing an email, I find it helps to have a mail client that will let you see the raw email message. Here is a message I was testing a while back. It is multi-part/alternative, but I think you will get the picture (I've stripped out some custom headers and some of the content to reduce the amount of text here): Received: from omta10.emeryville.ca.mail.comcast.net ([76.96.30.28]) by qmta07.emeryville.ca.mail.comcast.net with comcast id oSaB1i0030cQ2SLA7Sfnc3; Thu, 22 Mar 2012 02:39:47 +0000 To: [email protected] Subject: Micro Automation Development Password Reset From: MAD Website <[email protected]> X-Mailer: PHP/5.2.6-1+lenny10 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="====M A D==c5a2f38cae4428fc8c53cb0b245b7bed====" Date: Wed, 21 Mar 2012 21:39:45 -0500 This is a multi-part message in MIME format. --====M A D==c5a2f38cae4428fc8c53cb0b245b7bed==== Content-Type: text/plain; charset=charset="ISO-8859-1" Content-Transfer-Encoding: 7bit You are receiving this message because you (or someone using your email ... (several lines of content removed) ... violation of the Terms of Use (http://www.polly.tst/Site/TermsOfUse). Your secret code is: FC-4f6a90f1c41791-79000538 . This code is only valid for 24 Hours. After that time, the request will be discarded. You should go to http://www.polly.tst/User/ResetPassword and complete the password change as soon as possible. --====M A D==c5a2f38cae4428fc8c53cb0b245b7bed==== Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <HTML> <HEAD> <TITLE>Micro Automation Development Password Reset</TITLE> </HEAD> <BODY> <STYLE> P.MAD { margin: 1em; } </STYLE> <P class="MAD"> You are receiving this message because you (or someone using your email address) indicated ... (several lines of content removed) ... they may just be playing games with you, but that would be a violation of the <A href="http://www.polly.tst/Site/TermsOfUse">Terms of Use</A>. </P> <P class="MAD"> Your secret code is: <B><TT>FC-4f6a90f1c41791-79000538</TT></B>. This code is only valid for 24 Hours. After that time, the request will be discarded. You should go to <A href="http://www.polly.tst/User/ResetPassword/FC-4f6a90f1c41791-79000538">http://www.polly.tst/User/ResetPassword</A> and complete the password change as soon as possible. </P> </BODY> </HTML> --====M A D==c5a2f38cae4428fc8c53cb0b245b7bed====-- Lines 1 - 10 are the headers. You will note that PHP puts in lines 4 (To) and 5 (Subject) from parameters to the mail() call. The system mailer adds line 10 (Date). The first header (lines 1-3) is from one of the servers that handled the message. Each server that handles a message is required to add a Received: header -- there were actually four in this message. This is one of the reasons I am so adamant about following the specs. The mail servers have to add a header and if the line endings are not right, this could create problems. Lines 6, 7, 8 and 9 are the headers that I added with the headers parameter to the mail() call. That is all that goes in the (main) headers. The rest is all part of the body of the email. Well except line 11: Line 11 Is the blank line that says, "we are done with the headers, everything that follows is the message body". Lines 12 - 14 are lonely In a MIME message, anything before the first boundary is ignored. The text here is generally for anyone who does not have a MIME-compliant client. This is pretty much all they will be able to read easily. "In the old days", we would save the message to a file and run a program that understood MIME. (Does anyone really understand mimes?) Line 15 is the first boundary, so what follows it, up to the next boundary is one MIME part. Lines 16 - 17 are what I call the "header" of this part. It tells how this part is to be handled Line 18 is a blank line saying we are done with the MIME's head ... er ... header, and about to start on what I call the "body" of this part. Line 28 Is the boundary ending the first part and starting the second part. Line 59 Is the boundary ending the second part and ending the MIME content (because of the double-hyphen on the end). One thing to note. The CRLF just before the boundary (lines 14, 27, and 58). Even though we think of this CRLF as ending that blank line, it is actually considered part of the boundary. So, if the line is not blank --- if the second boundary were on Line 27 (in this example) --- then the first body-part does not end with a CRLF. Usually this is not significant, but may make a difference in some cases. (don't you hate it when mime body-parts get cut short like that)
-
Several points to note: 1) Please be more specific when asking for help. "It does not work", does not help us help you. What does it do that it should not do? What does it not do that it should do? Have you checked your "Junk" folder? Does it print the "mail could not be sent" message? 2) The Email specifications say that headers are to be separated by carriage-return--line-feed pairs (CRLF or "\r\n"). Yes, some, if not most, systems will try to handle emails that only use LF, but you never know what servers the message will pass through and what clients will be used to read it. It is best to follow the specs as closely as possible. 3) The MIME body of the message is broken into parts, separated by the boundary. I believe the specification implies that these lines, too, need to be separated by CRLF not LF alone. At any rate, there must be a blank line between the "header" of each part and the "body" of each part; and there must not be a blank line between the boundary and the first header in each part. 4) The LAST boundary indicates that the MIME message is complete. It must end with two hyphens (as well as start with two hyphens). 5) Remove AND FORGET the @-operator from the call to mail(). Forget you ever heard of it. It only hides errors, it does not magically fix them. If a statement is reporting errors/warnings/notices, you should fix the cause, not hide them. 6) The From: header needs to specify an email address that the sending server has authority to send mail for. It is not clear from your post if you are using a "user's" email address or your website's address. 7) The To: address needs to be a comma-separated list of valid email addresses. We cannot tell from your post what this variable contains. Having said all that, I think the problem is with #4; or #3 or #2. If it is not there then it is likely #1 or #5
-
Actually, mysql_result allows the row number to be specified as the second parameter. After my first post, I got to thinking that that query should not have failed. However, it will most likely return zero rows. So the call the mysql_query() is not able to return results. I've never used that function (preferring mysql_fetch_assoc), so I'm not sure what happens if you call it on a result set with no rows. @OP The mysql extension has been deprecated. If you are working on a new application, it is highly recommended that you switch to mysqli (with an "i" on the end) extension.
-
That error indicates that the query failed. There is a syntax error in the query. The best way to debug these things is to build the query in a separate variable, so you can echo it if the query fails, to see what the query actually says: $sql = "SELECT `premium` FROM `users` WHERE 'user_id' = '".$_SESSION["user_id"]."'"; $premium = mysql_query($sql); if ($premium === false) { // IN DEVELOPMENT - tell me what went wrong echo $sql . '<BR>' . mysql_error(); } else { $premium1 = mysql_result($premium, 0); By the way, you have single-quotes around the column name "user_id" in the WHERE clause. You probably meant to use back-ticks. However, the back-ticks are only needed if the column name is a reserved word. Personally, I don't use them at all.
-
Since $date_string is not an array, that code should report an error. To use array_unique() you would need to collect all of the week numbers in an array, then call it against that array. On the other hand, you should select DISTINCT dates so you don't process the same date multiple times. In fact, you could do the week number and distinct in the query (mySql WEEK): SELECT DISTINCT WEEK(Date) AS WeekNo FROM maildata ORDER BY 1 ASC
-
The path for include is relative to the current working directory. This is usually the path of the original executing script (in this case index.php). So include('config.php');, even inside of App.php, is looking in the Public directory not the Framework directory. Why it does not throw an error, could be a couple of things: there is another config.php in the Public directory; or your error reporting value does not cover this warning; or there is a config.php somewhere else in the include_path (from the PHP.ini file).
-
$sql = 'SELECT ...'; $results = mysql_query($sql) or die(mysql_error() . '<BR>' . $sql); Should show you if there is an error in the SQL statement. On the other hand: you misspelled the variable name in the while loop $results is plural in the query, but singular in the fetch.
-
Setting in phpMyAdmin? This is driving me crazy!
DavidAM replied to blackenedheart's topic in PHP Coding Help
And forget you ever heard of the error suppression operator ("@"). Your connection and database selection are using the mysql extension.Your fetch is using the mysqli extension Your query is calling a function that does not exist, but you have suppressed the error -- looks like you tried to use mysqli. But as Barand said you can't mix them. Use one or the other, and since the mysql (no "i") extension is deprecated, you should use mysqli -
What?? I'm not sure I understand what you are saying is happening or what you want to happen. Especially since your code is inserting the same data twice; that is definitely going to give you duplicates. Give us a clearer explanation of what the script should be doing, and what it is actually doing. Then show us some real code. We may be able to help. And, by the way, you need to sanitize those user inputs: mysql_real_escape_string for the text, validate that the numerics are valid numbers (floats/ints)
-
You have to check to see if the form was POSTed <?php if (isset($_POST)) { // Process user supplied data } To populate the fields, you have to echo them:<input type="number" name="AdSubtotal" id="AdSubtotal" class="medium" value=<?php echo $SubTotal; ?> /><br /> Stop using short tags ("<?") while you are still learning. By default, they are turned off, which can prevent your script from working.
-
cron job to launch php page which send e-mail
DavidAM replied to 09027882's topic in PHP Coding Help
Running a script in a cron job is not the same as accessing it through the browser. As a cron job, it does not go through the web-server so; the effective user is different (which can cause permission issues), the environment is different (which can cause includes to fail), the super globals ($_SERVER, $_GET, etc) are different or missing. In a normal *nix system, any output from a cron job is automatically emailed to the user who scheduled it. In a shared hosting environment, I would guess this is the email address you registered with. Try adding error reporting to the top of the script. And see what is failing: error_reporting(E_ALL); ini_set('display_errors', 1); If the output email doesn't work, you can log errors to a file, but you have to use an absolute path name (like you did for the script in the crontab). -
problems getting mysql, mysqli, and mcrypt to load
DavidAM replied to mathman54's topic in PHP Installation and Configuration
You have the "thread-safe" version of PHP, but the not thread-safe version of mysql (and mysqli). They have to match. I don't know where you downloaded from, but there is a pretty good explanation here http://windows.php.net/download in the side bar "Which version do I choose?" I'm not sure about the other modules, check and see if the dll's exist at the location specified in the error message. If not, you will need to get them or change the Apache configuration to look elsewhere. I don't think the double-slashes is a problem, but you can try fixing that in the Apache configuration as well (if the files do exist). If they are not in the config file that way, then it is probably a quirk in the error process. I would fix the thread-safe issue, and then see if the errors go away. I'm not going to be much more help than this, I have only ever setup a WAMP server one time, and the install was flawless (I usually run on Linux). However, I read through that page the other day because I needed PHP (command-line) to run on a Windows box.- 1 reply
-
- loading extensions
- mysql
-
(and 1 more)
Tagged with:
-
What is it that you are actually looking for? I think requinix's answer assumes you want the array index of the parent comment, which is the logical assumption. However, your function is simply walking up the list of comments looking for someone with no parent. Depending on how the data is generated, this may or may not be the parent of the comment you started with. In addition, you stated "I only allow one level of replies, so no reply to a reply" -- however, the comment at index 4 has a parent_id of 2; comment_id 2 is at array index 1 and has a parent_id of 1; which seems to me to say that 4 is a reply to 2, which is a reply to 1. If you are trying to find the parent of a specific comment, the solution is different from trying to find the previous top-level comment. I think I might load the array differently, but again, that depends on what you need to accomplish from this dataset.
-
Since they don't exist (in the database) you can't SELECT them. Well, you can write some complex code to get what you want, but I think the best approach is to create the monthly dates in the PHP script. However, once you have these dates, and mark them paid, and insert them into the database, you are going to have to include those already Paid dates in the query, and have the PHP script NOT generate them again. Maybe if you tell us the layout of the tables involved (concerning the dates), we can offer an approach that won't create problems latter. Edit: It's crazy how this Psycho keeps beating me to the punch!
-
Actually (from the manual fgets) (emphasis added) The problem is at the end of the input file (also from the manual) (emphasis added} Usually, the code would be something like: while ($str = fgets($this->in)) { // Process string } although, I usually just use file and foreach