Jump to content

requinix

Administrators
  • Posts

    15,229
  • Joined

  • Last visited

  • Days Won

    427

Everything posted by requinix

  1. I don't see anything that would cause an infinite loop but I do see a few things to fix. Can you do some debugging to find out where the problem is? I can improve upon what I said earlier but now it's probably getting hard to follow me. So I'll just throw the whole thing at you. function shorten($text, $limit) { $selfclosing = array("img", "br"); $parts = preg_split('#(</?([a-z]+)[^>]*>)#i', $text, -1, PREG_SPLIT_DELIM_CAPTURE); $what = "text"; // "text", "html", or "tag" $tagaction = "add"; // "add", "remove", or "ignore" $length = 0; // so far $opentags = array(); // stack of tags needing to close $output = ""; // shortened version foreach ($parts as $p) { // just regular text if ($what == "text") { // if the new $p pushes the $length too long, cut it and stop // this is a good place for an ellipsis $l = strlen($p); if ($length + $l >= $limit) { $output .= substr($p, 0, $limit - $length - $l) . "..."; break; } // otherwise add it else { $output .= $p; $length += $l; } $what = "html"; // next step } // the entire html tag. see if it needs a separate closing tag else if ($what == "html") { // if it's a closing tag then it needs to be removed from the stack if ($p[1] == "/") { $action = "remove"; } // if it explicitly closes itself then ignore it else if (substr($p, -2, 1) == "/") { $action = "ignore"; } // otherwise it's an opening tag so add it else { $action = "add"; } $output .= $p; $what = "tag"; // next step } // just the tag name else { // maybe add the tag to the top (beginning) of the stack (array) if ($action == "add" && !in_array(strtolower($p), $selfclosing)) { array_unshift($opentags, $p); } // remove whatever's on top else if ($action == "remove") { array_shift($opentags); } $what = "text"; // reset } } // now close off the remaining open tags foreach ($opentags as $tag) { $output .= "</{$tag}>"; } return $output; } If I run that on <div itemprop="commentText" class='post entry-content '> Hello all,<br /> I'm recently writing a script on the homepage that would display certain threads from certain forum categories.<br /> My current SQL query and fetching the contents work well, except I encounter an odd issue when using the substring method on the fetched contents to limit the characters displayed.<br /> Just so you're aware, I'm parsing the contents of the thread's post through vBulletin's BBCodeParser, yet that's not the issue.<br /> <br /> Here's a bit of background regarding my code/issue.<br /> Current Code (only included the important stuff):<br /> <pre class='prettyprint'> $parsed_text = $parser->do_parse($body); $message_pre = substr($parsed_text, 0, 500); $message = substr($message_pre, 0, strrpos($message_pre, ' ')); echo '<div id="a1"> echo '<div class="b">'; echo $message.'...'; echo '<div class="c"></div>'; echo '<div class="d">[<a href="">Read More...</a>]'; echo '</div>'; echo '<div class="e"></div></div></div>'; </pre> <br /> So, that's all fine. However, let's get some example database contents:<br /> (a modified piece of the HTML source of your post) with a length of 250 I get <div itemprop="commentText" class='post entry-content '> Hello all,<br> I'm recently writing a script on the homepage that would display certain threads from certain forum categories.<br /> My current SQL query and fetching the contents work well, except I encounter an odd issue when using the substring method on t...</div>
  2. Yeah, I think it was a typo in the post. The syntax error would have prevented the insertion entirely and the die() below it would have kicked in.
  3. this.value is easier. 'sub_cat_selector.php?id='+this.value Have you checked that sub_cat_selector.php is being invoked? With the right ID? And is correctly doing whatever it's supposed to be doing? [edit] Oh, and remove the "javascript:" from the onchange. That's only for link hrefs. onchange contains Javascript code already, you don't have to tell it again. And actually that may be the problem... [edit 2] Oh, and try to avoid inline Javascript. If you have something like jQuery then you can quite easily do a <select name="top_cat<?php echo $r['product_id'];?>" id="top_cat<?php echo $r['product_id'];?>" class="top_cat"> $(function() { $("select.top_cat").change(function() { MyAjaxRequest(this.id.replace(/top_cat/, "sub_cat_selector"), "sub_cat_selector.php?id=" + this.value); }); });
  4. Unless there are 1M rows to consider. Or even 10K. [edit] Shuffling the array, which you didn't say but would be a fundamental requirement to the solution, doesn't help. It's the same problem: it'll be random every single time. srand() might affect shuffle(), I don't know, but if so that would be a solution to that... except if you're seeding the RNG you might as well do it in MySQL where it'll perform better.
  5. No offense but there's more wrong than just this login issue. Which I'll get to. But there are other things you should know about and fix. In no particular order: 1. MD5 is not appropriate for a password. It's much less secure than it was, say, 10 years ago (which is not to say it was secure back then either). And two MD5s is actually worse. Use a better hashing algorithm, like SHA-256 or bcrypt, as well as good principles like salting/HMAC. If you're not sure about this then find a library like PHPass which can handle most of the work for you. 2. You're putting $_POST values directly into SQL which opens you up to a vulnerability called SQL injection. That's bad. Putting that aside for a second, the mysql extension and its mysql_* functions are old and shouldn't be used if at all possible. There are better replacements like mysqli and PDO that are faster, easier, and safer to use, and if you use the prepared statements functionality they provide then you don't even have to worry about SQL injection. 3. The problem I think you're encountering now is that you check for any user named $checkusername and then you check for any user with the given password. You aren't actually checking if the $checkusername user's password is $checkpassword - any user, any password. More specifically you expect there to be exactly one row for both queries; there's surely only one user with that username, but multiple users could have the same (probably simple) password. You can get the user information and then compare the password, but... 4. Presenting two different messages depending if the user exists or if the password is correct is unsafe. As a malicious user with a vendetta against someone I know, I could try to find his account on your site by trying many usernames. Once I get the "Wrong Username" message I'll know I found it and can focus on the password instead. You should only present one message for both cases; if you change your SQL to find a matching user and matching password, at once, then it will save you a lot of repetitious code. Two birds with one stone. 5. Although session data is generally safe, you shouldn't put the password in the session. You really don't need to either: if you only keep the username then you can rest assured that the only way the username got in the session is if they logged in correctly. Besides, how often would you need to know the password?
  6. That's JSON. Specifically it's an array.
  7. He's not the only one.
  8. find_by_sql() isn't returning anything: $return = $result_set;
  9. Browsers can remember form details but I have yet to see one (actually the browser itself) actually log someone in automatically. They're not obsolete. Normal cookies are just session cookies and if I close the browser the cookie is gone and I'm logged out. "Remember me" instructs the website to create another, non-session cookie (or more than one), containing enough data so that someone can be automatically logged in when the session cookie isn't present. The two serve similar but different purposes.
  10. What is "correctly" supposed to be? How does it look now and how do you want it to look?
  11. The only problem is the cut-off tag? Not the things? Technique I use is a preg_split() to alternate strings that can be cut (regular text) with strings that cannot (ie, HTML tags). As you're going you keep track of what HTML tags you've opened and closed. $parts = preg_split('#capture opening *and closing* html tags#', $input, -1, PREG_SPLIT_DELIM_CAPTURE); $cut = true; // first in $parts is regular text $length = 0; // so far $opentags = array(); // stack of tags needing to close $output = ""; // shortened version foreach ($parts as $p) { if ($cut) { // if you need to trim then go ahead, then break out of the loop // otherwise add to $length } else { // look at the captured html tag // if it opens and doesn't self-close then // - add the tag name to $opentags // if it closes then // - optionally check that it agrees with the top of the $opentags stack // - pop off $opentags } $output .= $p; $cut = !$cut; } // now close off the remaining open tags foreach ($opentags as $tag) { $output .= "</{$tag}>"; }
  12. RAND() allows you to seed the generator. Pick a random number, save that somewhere, and use it every time. $query = "SELECT * FROM `businesses` ORDER BY RAND($random) LIMIT $startResults, $perPage"; [edit] This is fine if you don't have too many results; this query is bad and will take a relatively long time so if not then storing the results in a table (with some identifying information so you know whose results they are) is better.
  13. Those two URLs are the same.
  14. $arr["a3f9a1ba-85a2-4142-b0d6-6a85bef30cb5"][0]["value"] = $newdata;
  15. How valid does it have to be? The extension is a good starting point. Otherwise the fileinfo extension can tell you the MIME type for a file (because the one given in $_FILES isn't trustworthy) but it takes a bit of work to set it up.
  16. >5 minutes ago, yes. It's not as foolproof as storing the time from the last alert but it'll probably be good enough.
  17. Store the time of the most recent alert somewhere, like a database, then only send the message if there's anything newer.
  18. Finally got it working from cmd.exe. powershell -command get-wmiobject win32_service -computername SERVER1 -filter \"startmode='auto'\" So then you have to escape the backslashes and the quotes if you do it from PHP. "powershell -command get-wmiobject win32_service -computername SERVER1 -filter \\\"startmode='auto'\\\""
  19. And you've verified that the command itself works? You're using "s around the entire command which means you [a] don't use them inside the string, if you even need them in the first place, or escape them. system("powershell -command get-wmiobject win32_service -computername SERVER1 -filter startmode=auto"); [edit] Tried it, your command doesn't work. I can't figure out how to get it to accept quotes in the SQL it generates. This does work: Get-WmiObject win32_service -ComputerName SERVER1 | Where { $_.StartMode -eq "Auto" }
  20. Easiest method? Stick a $replaced at the beginning, a $replaced at the end, and replace an empty line (probably two consecutive newline sequences) with a $replaced+two newlines+$replaced.
  21. Or PicNum to number. Or change them both to "verdruss". Doesn't matter as long as they're the same.
  22. No. Your link is putting the ID in as "PicNum" but your script is looking for it as "number".
  23. What do you mean by "reload"? Are you destroying and/or recreating the a#java-fancy-window? It'll lose the onclick event you have written there inline.
  24. Well... yeah. That's the key of the "array" you're looping over. The name of the node. You don't want the name.
×
×
  • 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.