crazycoder
09-02-2006, 05:04 PM
I need to be able to take a number in a string and convert it into a numeric variable. Eval doesn't work and I can't find a function to do it. :(
|
Click to See Complete Forum and Search --> : eval crazycoder 09-02-2006, 05:04 PM I need to be able to take a number in a string and convert it into a numeric variable. Eval doesn't work and I can't find a function to do it. :( crazycoder 09-02-2006, 05:08 PM Actually, it's a counter so glabal variables would work, too. Also, it would be cool if I could find out if they're a unique visitor. DJsAC 09-02-2006, 05:39 PM <? $number = "24924952"; $content = "this is in the number string"; $evalme = '$'.$number.' = '.$content.';'; eval($evalme); echo $24924952; // echos: this is in the number string should work :) NogDog 09-02-2006, 06:02 PM It's not clear to me exactly what you want to do. If DJaSC's answer is not what you're looking for, perhaps you could give a bit more explanation, including sample before and after data? crazycoder 09-02-2006, 08:08 PM I want to load the number from a file as a string. Then, increment it then echo it. I think DJsAC's answer is backwards, when I have the string it returns a number. like: eval('a' = implode('', file('theFile.txt'))); //somehow increment that's fine, I can use the string if I'm just echoing but I can't increment a string DJsAC 09-02-2006, 08:15 PM /* file.txt: ---------- 2949253 ---EOF--- */ $content = file_get_contents('theFile.txt'); echo $content; /*2949253*/ $content += 0; /*convert content to number type*/ $content++; /*increment 1*/ echo $content; /*2949254*/ NogDog 09-02-2006, 09:47 PM Just wanted to see if I could do it as a one-liner. :) <?php $a = trim(file_get_contents("theFile.txt")) + 1; echo $a; ?> crazycoder 09-03-2006, 11:44 AM That's magic but how do I replace the contents of the file? DJsAC 09-03-2006, 11:47 AM http://nl2.php.net/manual/en/function.fwrite.php <?php $filename = 'test.txt'; $somecontent = "Add this to the file\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> crazycoder 09-03-2006, 11:57 AM will that not add to the file instead of overwrite? DJsAC 09-03-2006, 11:59 AM will that not add to the file instead of overwrite? change the 'a' to 'w' in the fopen function. crazycoder 09-03-2006, 12:13 PM I have this: <?php if(file_exists('theFile.txt')) { if(is_writable('theFile.txt')) { $hits = file_get_contents('theFile.txt'); $hits += 0; /* converts to num */ $hits++; if(!$handle = fopen('theFile.txt', 'w')) { echo -1; } if(fwrite($handle, $hits) === FALSE) { echo -1; } else { echo $hits; fclose($handle); } } else { echo -1; } } else { echo -1; } ?> that gives me -1 DJsAC 09-03-2006, 12:17 PM if you change the -1 to -2, -3 -4 etc it makes debugging a whole lot easier... your problem is probably that it can't write to the file ;) check to see if you have write permissions. and if you're going to open the file, you might as wel use fread() instead of file_get_contents() ;) crazycoder 09-03-2006, 12:28 PM you're right, I changed it to this: <?php $file = 'theFile.txt'; if(file_exists($file)) { if(is_writable($file)) { $hits = file_get_contents($file); $hits += 0; /* converts to num */ $hits++; if(!$handle = fopen($file, 'w')) { echo -1; } if(fwrite($handle, $hits) === FALSE) { echo -2; } else { echo $hits; fclose($handle); } } else { echo -3; } } else { echo -4; } ?> that gives me -3, do I need to log into ftp or something? crazycoder 09-03-2006, 12:33 PM never mind I got it to work thanks actually, one more queation, sorry but, I want it to count unique visitors, should I just use a cookie that expires every year or what? crazycoder 09-03-2006, 12:42 PM here's my final masterpiece (with much thanks to NogDog and DJsAC): <?php $file = 'theFile.txt'; if(file_exists($file)) { if(is_writable($file)) { if(!isset($_COOKIE['visited'])) { setcookie('visited', 'true', time()+60*60*24*366); $hits = file_get_contents($file); $hits += 0; /* converts to num */ $hits++; if(!$handle = fopen($file, 'w')) { echo -1; } if(fwrite($handle, $hits) === FALSE) { echo -2; } else { echo $hits; fclose($handle); } } else { /* ha! thought you'd come back again! foolish mortal! */ echo file_get_contents($file); } } else { echo -3; } } else { echo -4; } ?> DJsAC 09-03-2006, 12:49 PM a better way would be to save their IP to another file, and check the visitors IP against that list. Some users clear their cache after every browsing session and thus every visit to your site would still count ;) The IP's however don't change in most cases, which means they already visited, and can't change that, even if they empty their cache. (Unless you use AOL which gets a different IP at ever request - supposedly) crazycoder 09-03-2006, 04:12 PM How do I get their IP, though? DJsAC 09-03-2006, 04:15 PM <?php $ip = $_SERVER['REMOTE_ADDR']; ?> crazycoder 09-03-2006, 05:21 PM I have this now: <?php /* much thanks to NogDog and DJsAC from webdeveloper.com */ $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; if(file_exists($ipfile)) { if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { $hits = -5; } else { $iparray = file($ipfile); } } else { $hits = -6; } } else { $hits = -7; } if(file_exists($file)) { if(is_writable($file)) { if(!in_array($ip, $iparray)) { if(fwrite($iphandle, $ip . '\n') === FALSE) { $hits = -8; } else { $hits = file_get_contents($file); $hits += 0; /* converts to num */ $hits++; if(!$handle = fopen($file, 'w')) { $hits = -1; } if(fwrite($handle, $hits) === FALSE) { $hits = -2; } else { fclose($handle); } } } else { /* ha! thought you'd come back again! foolish mortal! */ $hits = file_get_contents($file); } } else { $hits = -3; } } else { $hits = -4; } ?> I get the error that the $iparray variable is not an array :o and that 'supplied argument is not a valid stream resource' in line 29 crazycoder 09-03-2006, 05:44 PM Okay, I forgot my permissions again... crazycoder 09-03-2006, 05:53 PM <?php /* much thanks to NogDog and DJsAC from webdeveloper.com */ $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; if(file_exists($ipfile)) { if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { $hits .= -5; } else { $iparray = file($ipfile); } } else { $hits .= -6; } } else { $hits .= -7; } if(file_exists($file)) { if(is_writable($file)) { if(!in_array($ip, $iparray)) { if(fwrite($iphandle, $ip . '\n') === FALSE) { $hits .= -8; } else { $hits = file_get_contents($file); $hits += 0; /* converts to num */ $hits++; if(!$handle = fopen($file, 'w')) { $hits .= -1; } if(fwrite($handle, $hits) === FALSE) { $hits .= -2; } else { fclose($handle); } } } else { /* ha! thought you'd come back again! foolish mortal! */ $hits = file_get_contents($file); } } else { $hits .= -3; } } else { $hits .= -4; } ?> but when I look at the IPfile it's like this: 123.123.123.0\n123.123.123.0\n how do I actually put in the line break? NogDog 09-03-2006, 06:18 PM Your \n needs to be in double-quotes, not single-quotes, for it to be interpolated as a linefeed character. crazycoder 09-03-2006, 06:27 PM that works but it still increments when I hit refresh crazycoder 09-04-2006, 11:08 AM It puts them in the text file but more than once DJsAC 09-04-2006, 11:36 AM Try this: <?php $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; $error = "<hr />"; if(file_exists($ipfile)) { $iparray = file($ipfile); //as array if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { $error .= "IP File not openable.<br />"; } if(!in_array($ip, $iparray)) { if(fwrite($iphandle, $ip.'\r\n') === FALSE) { $error .= "Could not write IP file.<br />"; } else { $inarray = FALSE; // We have a new user. } } if(in_array($ip, $iparray)) { $inarray = TRUE; } } else { $error .= "IP File is not writeable.<br />"; } } else { $error .= "IP File does not exist.<br />"; } if(file_exists($file)) { $hits = file_get_contents($file); //as string if($inarray === FALSE) //new user? increment and save new count, else: don't change a thing { $hits += 0; // converts to num $hits++; // increment hits if(is_writable($file)) { if(!$handle = fopen($file, 'w')) { $error .= "Counter File not openable.<br />"; } if(fwrite($handle, $hits) === FALSE) { $error .= "Counter File could not be written.<br />"; } else { fclose($handle); } } else { $error .= "Counterfile is not Writeable.<br />" } } } else { $error .= "Counter File does not exist.<br />"; } echo "We have had '".$hits."' unique visitors to the website.<br />"; if ($error != "<hr />") { echo $error; } /* Completely Recoded by DJsAC Much Thanks to NogDog and DJsAC from www.WebDeveloper.com */ ?> crazycoder 09-04-2006, 12:58 PM I redid it to avoid echos: <?php /* completely recoded by DJsAC then edited again by me to avoid echo much thanks to NogDog and DJsAC from webdeveloper.com */ $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; $error = '<hr />'; if(file_exists($ipfile)) { $iparray = file($ipfile); if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { /* opens ip file */ $error .= 'failed to open ip file<br />'; } else { $inarray = false; } if(in_array($ip, $iparray)) { $inarray = true; } } else { $error .= 'ip file not writeable<br />'; } } else { $error .= "ip file doesn't exist<br />"; } if(file_exists($file)) { $hits = file_get_contents($file); if($inarray === false) { /* new user */ $hits += 0; /* converts to num */ $hits++; if(is_writable($file)) { if(!$handle = fopen($file, 'w')) { /* opens counter file */ $error .= 'failed to open counter file<br />'; } if(fwrite($handle, $hits) === false) { $error .= 'failed to write to counter file<br />'; } else { fclose($handle); } } else { $error .= 'counter file not writeable<br />'; } } } else { $error .= "counter file doesn't exist<br />"; } if($error === '<hr />') { $error = ''; } ?> I plan to use this syntax: <?php include('counter/counter.php'); ?> <?php echo $hits ?> <?php echo $error ?> but it increments by 2 crazycoder 09-04-2006, 01:00 PM Okay, that's strange. I didn't do anything and now it's incrementing by one again. Stranger, it increments when I refresh. DJsAC 09-04-2006, 01:08 PM you deleted a crucial part... (fwrite for ipfile) My code was 'echo-free' just delete the last 5 lines from the bottom, and save as counter.php then in the page: <?php include('counter/counter.php'); ?> and then either:<?php echo $hits ?> <?php echo $error ?> OR: <?php echo "We have had '".$hits."' unique visitors to the website.<br />"; if ($error != "<hr />") { echo $error; } ?> crazycoder 09-04-2006, 01:50 PM Okay, so I changed it: <?php /* completely recoded by DJsAC then edited again by me to avoid echo much thanks to NogDog and DJsAC from webdeveloper.com */ $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; $error = '<hr />'; if(file_exists($ipfile)) { $iparray = file($ipfile); if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { /* opens ip file */ $error .= 'failed to open ip file<br />'; } if(!in_array($ip, $iparray)) { $inarray = false; if(fwrite($iphandle, $ip . "\n") === false) { $error .= 'failed to write to ip file<br />'; } } if(in_array($ip, $iparray)) { $inarray = true; } } else { $error .= 'ip file not writeable<br />'; } } else { $error .= "ip file doesn't exist<br />"; } if(file_exists($file)) { $hits = file_get_contents($file); if($inarray === false) { /* new user */ $hits += 0; /* converts to num */ $hits++; if(is_writable($file)) { if(!$handle = fopen($file, 'w')) { /* opens counter file */ $error .= 'failed to open counter file<br />'; } if(fwrite($handle, $hits) === false) { $error .= 'failed to write to counter file<br />'; } else { fclose($handle); } } else { $error .= 'counter file not writeable<br />'; } } } else { $error .= "counter file doesn't exist<br />"; } if($error === '<hr />') { $error = ''; } ?> sometimes it increments by one other times it increments by two DJsAC 09-04-2006, 01:53 PM it CAN'T increment by 2 unless the page is loading twice... The code only contains $hits++; 1 time, so it will only add 1 ONE time... :s crazycoder 09-04-2006, 06:19 PM I know it must be my browser.. but I see it crazycoder 09-04-2006, 06:39 PM it's as if the in_array function's not realiable crazycoder 09-04-2006, 06:45 PM I tried writing another function but that one didn't work either crazycoder 09-04-2006, 08:01 PM I tried to use this code: <?php $cfile = 'theFile.txt'; $ipfile = 'IPfile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $error = '<hr />'; if(file_exists($cfile)) $cfexists = true; else $error .= 'counter file does not exist<br />'; if(file_exists($ipfile)) $ipfexists = true; else $error .= 'ip file does not exist<br />'; if(is_writable($cfile)) $cfwrt = true; else $error .= 'counter file is not writeable<br />'; if(is_writable($ipfile)) $ipfwrt = true; else $error .= 'ip file is not writeable<br />'; if($cfhandle = fopen($cfile, 'a')) $cfopen = true; else $error .= 'failed to open counter file<br />'; if($iphandle = fopen($ipfile, 'a+')) $ipfopen = true; else $error .= 'failed to open ip file<br />'; if($error === '<hr />') { $hits = file_get_contents($cfile); $ips = file($ipfile); if(!in_array($ip, $ips)) { $hits += 0; $hits++; if(!fwrite($cfhandle, $hits)) $error .= 'failed to write to counter file<br />'; else fclose($cfhandle); if(!fwrite($iphandle, $ip . "\n")) $error .= 'failed to write to ip file<br />'; else fclose($iphandle); } } if($error === '<hr />') $error = ''; ?> I recorded the increments: 1 2 13 1214 12131215 That is not what I expected crazycoder 09-05-2006, 03:35 PM Okay, that was really stupid of me, I thought I might need the booleans <?php $cfile = 'theFile.txt'; $ipfile = 'IPfile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $error = '<hr />'; if(!file_exists($cfile)) $error .= 'counter file does not exist<br />'; if(!file_exists($ipfile)) $error .= 'ip file does not exist<br />'; if(!is_writable($cfile)) $error .= 'counter file is not writeable<br />'; if(!is_writable($ipfile)) $error .= 'ip file is not writeable<br />'; if(!$cfhandle = fopen($cfile, 'a')) $error .= 'failed to open counter file<br />'; if(!$iphandle = fopen($ipfile, 'a+')) $error .= 'failed to open ip file<br />'; if($error === '<hr />') { $hits = file_get_contents($cfile); $ips = file($ipfile); if(!in_array($ip, $ips)) { $hits += 0; $hits++; if(!fwrite($cfhandle, $hits)) $error .= 'failed to write to counter file<br />'; else fclose($cfhandle); if(!fwrite($iphandle, $ip . "\n")) $error .= 'failed to write to ip file<br />'; else fclose($iphandle); } } if($error === '<hr />') $error = ''; ?> That still gives me the same problem DJsAC 09-05-2006, 03:51 PM latest script has fopen( "", 'a') which = append... I don't believe you want this for the counter file ;) as far as why it's double incrementing, I don't have a clue... crazycoder 09-05-2006, 04:06 PM What do you mean it says fopen($cfile, 'a')? DJsAC 09-05-2006, 04:10 PM if(!$cfhandle = fopen($cfile, 'a')) $error .= 'failed to open counter file<br />'; if(!$iphandle = fopen($ipfile, 'a+')) $error .= 'failed to open ip file<br />'; The in the fopen() function means to APPEND to the file.... NOT clean the contents and start over ;) DJsAC 09-05-2006, 04:31 PM I've tested it, the version below WORKS. And only increments by 1. <?php $file = 'theFile.txt'; $ip = $_SERVER['REMOTE_ADDR']; $ipfile = 'IPfile.txt'; $error = "<hr />"; if(file_exists($ipfile)) { $iparr = file_get_contents($ipfile); //as string $iparray = explode("\r\n",$iparr); //to array if(is_writable($ipfile)) { if(!$iphandle = fopen($ipfile, 'a+')) { $error .= "IP File not openable.<br />"; } if(array_search($ip, $iparray) === FALSE) { if(fwrite($iphandle, $ip."\r\n") === FALSE) { $error .= "Could not write IP file.<br />"; } else { $inarray = FALSE; // We have a new user. } } if(in_array($ip, $iparray)) { $inarray = TRUE; } } else { $error .= "IP File is not writeable.<br />"; } } else { $error .= "IP File does not exist.<br />"; } if(file_exists($file)) { $hits = file_get_contents($file); //as string if($inarray === FALSE) //new user? increment and save new count, else: don't change a thing { $hits += 0; // converts to num $hits++; // increment hits if(is_writable($file)) { if(!$handle = fopen($file, 'w')) { $error .= "Counter File not openable.<br />"; } if(fwrite($handle, $hits) === FALSE) { $error .= "Counter File could not be written.<br />"; } else { fclose($handle); } } else { $error .= "Counterfile is not Writeable.<br />"; } } } else { $error .= "Counter File does not exist.<br />"; } echo "We have had '".$hits."' unique visitors to the website.<br />"; if ($error != "<hr />") { echo $error; } /* Completely Recoded by DJsAC Much Thanks to NogDog and DJsAC from www.WebDeveloper.com */ ?> crazycoder 09-05-2006, 06:02 PM thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! thank you! DJsAC 09-05-2006, 06:05 PM :eek: Right :rolleyes: Sure, no problem :) webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |