Jump to content

NamemeNick

Members
  • Posts

    19
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

NamemeNick's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Bauer418, that's fantastic advice. Thank you, well taken.
  2. SOLVED. Thank you guys; using nl2br in conjunction with str_replace was key. The correct handling of the post data is: <?php $input=str_replace("\r\n","",nl2br(htmlentities($_POST["field"]))); ?> Using "\r\n" instead of "\n" as the first argument of str_replace made the difference, but I don't understand why.
  3. Thank you guys for the suggestions. I've tried both of them with no luck. Using nl2br alone places <br/> in front of the newline feed, but the newline feed is still present. <?php $input= nl2br(htmlentities($_POST['field']));?> Later, in javascript: <script type="text/javascript"> myFunction('<?php echo $input ?>'); </script> At runtime, javascript throws an "unterminated string" error because the newline is still there <script type="text/javascript> myFunction('line1<br /> line2'); </script> Using str_replace in conjunction with nl2br doesn't help: <?php str_replace("\n","",nl2br(htmlentities($_POST["field"]))) ?> At runtime, the argument for myFunction is still 'line1<br /> line2' and the "unterminated string" error stands I thought that maybe the newline was not matched to "\n" and so str_replace wasn't doing anything, so in php I tried: <?php str_replace("\n","GOTCHA",nl2br(htmlentities($_POST["field"]))) ?> In Javascript, the output generates an unterminated string error: <script type="text/javascript> myFunction('line1<br /> GOTCHAline2'); </script> The newline is still there. nl2br inserts <br /> before the newline, and str_replace enters GOTCHA after the newline (but doesn't remove the newline). This is driving me nuts. Any other suggestion?
  4. User fills a form that contains a textarea and I need help figuring out how to format the input string to store it in mysql for later retrieval and use in javascript. Suppose the user enters: line1 line2 When the form is posted, I read the field with $input=htmlentities($_POST['field']); Later, this input is used as argument in a javascript function: <script type="text/javascript">myFunction('<?php echo $input; ?>');</script> The problem is that javascript throws an "unterminated string" error because at run time, the browser sees a new line feed in the middle of the string like: <script type=text/javascript"> myFunction('line1 line2'); </script> My question is: how can I manipulate the input string so that input is on one line like "line1<br>line2" instead of "line 1 line2" ? I have tried using str_replace to read the $_POST data: $input=str_replace("\n","<br>",htmlentities($_POST['field'])); However this doesn't work. At runtime, the javascript becomes <script type="text/javascript"> myFunction('line1 <br>line2'); </script> <br> was put in, but the newline feed/character is still there, so the "unterminated string" error still comes up. Suggestions please?
  5. Thanks ghostdog, Someone brought parse_str() to my attention. Checkout http://us2.php.net/manual/en/function.parse-str.php
  6. Thanks guys! ghostdog74, it seems like your script will return an array rather than distinct variables holding strings. Is that right? What do you think of this one? I got it from another source but it looks like gibberish to me (seems to work though) $querystring = 'height=6&weight=170&eyes=brown'; preg_match_all("/(.*?)=(.*?)(&|$)/", $querystring, $matches); for ($i = 0; $i < count($matches[0]); $i++) $$matches[1][$i] = $matches[2][$i];
  7. This seems like a simple problem but I'm a newbie struggling What is the most efficient way to extract key-value pairs from a given string? For instance I'd like to go from: 'height=6&weight=170&eyes=brown' to $height='6'; $weight='170'; $eyes='brown'; Please teach :-)
  8. for the past 3 weeks, I've used $_SERVER['REMOTE_ADDR']) to access the visitor's IP address without problems. Today, it returns the incorrect 192.168.151.35 (this is a private address ie behind router I think). I have no idea why this is happening, as I know that is not my IP address (www.whatismyip.com shows correct IP). Please check out whether my page reads your address correctly and let me know at www.gmatfix.com/demo Under what condition can $_SERVER['REMOTE_ADDR']) return an incorrect IP? How can I fix it? Thank you
  9. If anyone is interested, my cookie read failed everytime because the "." character is not allowed in PHP cookies. In Javascript, document.cookie returned ip_file.php=1 In PHP, var_dump() returned array(1) { ["ip_file_php"]=> string(1) "1" } The two refer to the same cookie but they have different names! In my php coding I was looking for "ip_file.php". That's why the cookie read failed every time :-) Thanks to Deidre's Dad and JustSomeGuy at the W3Schools forum for figuring out what was wrong (http://w3schools.invisionzone.com/index.php?showtopic=24027&st=0&gopid=132729&#entry132729)
  10. Thanks KingPhillip, I used phpinfo to find out that I can indeed use file_get_contents(). I've tried file_get_contents instead of fread() to get the contents in a string, and I've tried using fseek() as well. These unfortunately have not made a difference. The problem hasn't been reading from or writing to the file (I've tested those successfully). The hitch in the code (I believe) comes in checking whether the cookie exists. I started going crazy and wondered whether maybe the server was just ignoring the else condition, so I changed it from "isset" elseif(isset ($_COOKIE[$ip_file])){fwrite($fp, " || ip == \"$ip\"");} to "!isset"... elseif(!isset ($_COOKIE[$ip_file])){fwrite($fp, " || ip == \"$ip\"");} I figured if the server is ignoring that line the change would make no difference. Of course I really was going crazy... Just as the isset() always failed, !isset() always returns true. Still don't know where I'm going wrong. Is there a more efficient way to accomplish my bottom line (without using a login script)? I have a list of ip addresses and I want people who initially had those ip addresses to be able to access the members' page even after a change in ip. My current techniques relies on a cookie in tandem with the ip. if IP is in the file, set cookie. elseif ip is not in the file but cookie is present, add current ip to the file. I felt pretty clever about my code
  11. There are 3 files at play in my broken script. [*]the main file, index.php includes the script (script.php) on the first line, before even a single space character <?php ini_set('error_reporting', E_ALL); include("./script.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>main page</title> <script type="text/javascript"> ip = '<?php $ip=$_SERVER['REMOTE_ADDR']; echo $ip;?>'; window.defaultStatus = "Your IP address is "+ip; </script> </head> <body> <script type="text/javascript"> document.write("Cookies present are : " + document.cookie); </script> <br /> </body> </html> the script file, script.php looks for the user's IP in the 3rd file and updates the cookie if IP is found. Elseif IP is not found, this file looks for the cookie and if the cookie is found the file writes the user's IP to the 3rd file <?php ini_set('error_reporting', E_ALL); $ip=$_SERVER['REMOTE_ADDR']; ##File containing the authorized IP list $ip_file = "ip_file.php"; ## If current IP is found in the file, set "ip_file.php" cookie to "1"; else if proper cookie is found, add current IP to the file. #Read the file contents $fp = fopen($ip_file,"a+"); $contents = fread($fp, filesize($ip_file)); #If IP is found, save cookie. Elseif cookie is found, add IP to the file. if(strpos($contents,$ip)){setCookie($ip_file,"1",time()+3600*24*90,"/");} elseif(isset ($_COOKIE[$ip_file])){fwrite($fp, " || ip == \"$ip\"");} else { echo '<pre>'; print_r($_COOKIE); echo '</pre>';} #Close the file fclose($fp); ?> The last file, ip_file.php is just a list of IPs ip == "66.123.44.55" || ip == "44.556.45.443" To generate the error, do the following: [*]add your IP to ip_file.php [*]load index.php so that the script will set the cookie "ip_file.php=1" [*]delete your IP from ip_file and reload the page (without clearing cookies). Even though the cookie is present, your IP will not be written to ip_file.php like it should Thank you for your help.
  12. KingPhillip thank you for trying to help. I will try file_get_contents() and let you know how it works out. I use PHP4. It seems this function is for PHP 4.3 and above but I don't know how to check what my exact version of PHP is I have tried using fopen ($IP_file, "r+") as you suggested without success. You suggested that I change the fwrite call, but I know it already works because when I remove the conditional statement (the else statement) and just call fwrite($fp, " || ip == \"$ip\""); The write is performed without problem. The other issue you mentioned is that I don't check whether the cookie exists first with an isset() call in my elseif statement. I've tried using the isset before (and I've just tried again) but that hasn't helped me. It seems isset returns false even when the cookie exists. Your last advice is to have PHP print the cookies so I can see what they are. As you will see in my next post, I use Javascript to print the cookie in the main file document.write(document.cookie) I've taken your advice however and also printed the cookies from php. Indeed, the cookie shows as present even when isset() returns false! I am stumped. I will post the full code in my next posting.
  13. I've also tried putting an ob_start() at the beginning of script.php and ob_end_flush() at the end, but no luck.
  14. WolfRage, here is what I did 1) in php.ini, I set display_erros = on 2) at the top of script.php, I put ini_set('error_reporting', E_ALL); 3) In the else statement I put else {/*exception*/ header('Location: http://www.phpfreaks.com/forums/index.php/topic,244065.0.html')} When I load the page (with the cookie present), nothing happens (I'm not redirected). What does this mean? Is this helpful?
×
×
  • 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.