|
PHP
Feb 24, 2007 21:06:20 GMT
Post by FireIndy on Feb 24, 2007 21:06:20 GMT
Im kinda broadening my horizion on languages and took up PHP. I wondered who has done any PHP? Also, what are the possibilities with what you can do with PHP. Any examples of what it can do would be nice Edit : First PHP script: h1.ripway.com/fireindy/calculate.php
|
|
Ross
eCreations Staff
Posts: 1,768
|
PHP
Feb 24, 2007 22:08:01 GMT
Post by Ross on Feb 24, 2007 22:08:01 GMT
I've been using PHP + MySql for about 18 months now and must say that it's the most useful language I've learnt so far. Because it's a server side language, you don't have to worry about how to store long term information, you can just set yourself up either a flat file or MySql database in which to store it in.
As for the possibilities of PHP, when combinded with a client side language like javascript the possibilities are pretty endless. It can do everything from logging information about your visitors, storing/retrieving information given by visitors (eg. their comments), dynamically creating pages and even making on the fly images.
|
|
|
PHP
Feb 24, 2007 22:14:45 GMT
Post by FireIndy on Feb 24, 2007 22:14:45 GMT
Thanks Ross, I know about MySql, but what exactly is a flat file?
|
|
Ross
eCreations Staff
Posts: 1,768
|
PHP
Feb 25, 2007 1:15:04 GMT
Post by Ross on Feb 25, 2007 1:15:04 GMT
Thanks Ross, I know about MySql, but what exactly is a flat file? A flat file is basically a text file containning a string of information. It can be useful when using a script that a) has to be capable of running on multiple configerations (no messing around with setting up MySql database) b) Doesn't have to store much information such as a hit counter If you want to, check out some of the functions like fopen(), fwrite(), fclose() and file_get_contents()
|
|
Danny
Posts Too Much
Formerly Schnooble
Posts: 332
|
PHP
Feb 25, 2007 2:31:37 GMT
Post by Danny on Feb 25, 2007 2:31:37 GMT
FireIndy, By any chance did you use this for you adding script ?
<?php echo ($_POST["val1"] + $_POST["val2"]); ?>
|
|
|
PHP
Feb 25, 2007 2:45:33 GMT
Post by FireIndy on Feb 25, 2007 2:45:33 GMT
Well kinda..
I did :
$result = $_POST[val1] + $_POST[val2];
And just echoed $result
|
|
|
PHP
Mar 7, 2007 0:50:18 GMT
Post by FireIndy on Mar 7, 2007 0:50:18 GMT
Ok i gots a question. If I wanna replace a value in a flat file how would I do it. Each value is on a new line. Would I use fread() or something like that. I have a variable I want to find in the text and replace it, then resave the file.
|
|
Ross
eCreations Staff
Posts: 1,768
|
PHP
Mar 7, 2007 1:04:10 GMT
Post by Ross on Mar 7, 2007 1:04:10 GMT
Ok i gots a question. If I wanna replace a value in a flat file how would I do it. Each value is on a new line. Would I use fread() or something like that. I have a variable I want to find in the text and replace it, then resave the file. The problem with flat files is that you'd have to re-write the entire thing. For example: Contents of me.php:
ross 0000000000 Hello World
// Path to the file we want to edit $path = 'files/me.php';
// Get contents of curent file $file = file($path); // Change the value we want to change, in this instance, we want to set it to the current time $file[1] = time(); // Pretty much the opposite of file(), it joins the array back into a string. In this example, we want a new line between each element of the array which is what the first argument tells it to do. $file = implode('\n', $file); // Write the new string to the file if($handle = ($path, 'w')) { fwrite($handle, $file); fclose($handle); }
Contents of me.php:
ross 1173229251875 Hello World
|
|
|
PHP
Mar 7, 2007 2:11:17 GMT
Post by FireIndy on Mar 7, 2007 2:11:17 GMT
I tried something, but it doesnt want to work. Heres what I have...
$path = "usersonlinedata.txt"; $file = file($path); for($i=0;$i<count($file);$i++){ if ($file[$i] == $_COOKIE['username']){ $file[$i] = ""; $file = implode("\n", $file); if($handle = fopen($path, 'w')) { fwrite($handle, $file); fclose($handle); } } }
It loops through the lines, looking for the variable, and resets it to nothing, but I have a question.
You had this:
if($handle = ($path, 'w')) { fwrite($handle, $file); fclose($handle); }
You set the file to "w" which deletes everything doesnt it? So wouldnt that just rewrite a blank file?
|
|
Ross
eCreations Staff
Posts: 1,768
|
PHP
Mar 7, 2007 12:27:50 GMT
Post by Ross on Mar 7, 2007 12:27:50 GMT
I used 'w' because we don't need what is already in the file since we're going to be rewritting it. All the information that was in the file is now stored in the $file variable.
As for your other problem, I've just realised that when you use file() it includes the new line. ie. instead of $file[$x] being just "Hello World", it is "Hellow World ". The best way to get around this is to use preg_match() to see if the value is equal. eg.
$path = "usersonlinedata.txt"; $file = file($path); for($i=0;$i<count($file);$i++){ // I'd recommend using a variable as the username rather than just pulling the direct cookie. You also need to ensure that you strip any non-acceptable characters from user-submitted data before actually doing anything with it. if (preg_match("/^$username(\n|\r)+$/", $file[$i])) { $file[$i] = ""; // Since \n is already appended to each line, you don't need to add it back in to join it. $file = implode("", $file); if($handle = fopen($path, 'w')) { fwrite($handle, $file); fclose($handle); } } }
|
|
|
PHP
Mar 7, 2007 13:38:04 GMT
Post by FireIndy on Mar 7, 2007 13:38:04 GMT
Thanks Ross, I finally figured it out.
|
|
|
PHP
Mar 11, 2007 21:23:36 GMT
Post by FireIndy on Mar 11, 2007 21:23:36 GMT
Hey I have a question. My users info is stored like this Username|##|Other Info|##| I am trying to modify their profile. I have a page setup. There is a text box called AIM_screename. Would something like this work? I want it to find the line with the username then loop through the line to find "AIM - ".
What I did was: Open userdata.txt for reading. Seperated each line into an array. Searched the array for thier username Replace the AIM - with AIM - (+ the textbox text)
<? $user = $_COOKIE['username']; $filename = "userdata.txt"; $fd = fopen ($filename, "r"); $contents = fread($fd, filesize($filename)); fclose ($fd); $contents = explode("\n", $contents); foreach( $contents as $key ){ if(strstr($contents,$user)){ $AIM = explode("|##|",$key); foreach($key as $Aim1){
$Aim1 = "AIM - " . $_POST['AIM_screename']; $fd = implode("", $fd); if($handle = fopen($filename, 'w')) { fwrite($handle, $fd); fclose($handle); } } } } header("Location: action=myprofile.php"); ?>
Edit : It looks like it works, but when i view the user info, nothing has changed.
|
|
|
PHP
Mar 26, 2007 1:29:43 GMT
Post by FireIndy on Mar 26, 2007 1:29:43 GMT
Ive gotten better , well at least i think so..... Ive been trying to incorporate UBBC, so i gave it a go. h1.ripway.com/fireindy/Post.html <? if(preg_match("/\[b\](.+?)\[\/b\]/",$_POST['post_form'],$post_array)){ $var = preg_replace("/\[b\](.+?)\[\/b\]/","<b>$1</b>",$_POST['post_form']); echo "$var"; } if(preg_match("/\[i\](.+?)\[\/i\]/",$_POST['post_form'],$post_array)){ $var1 = preg_replace("/\[i\](.+?)\[\/i\]/","<i>$1</i>",$_POST['post_form']); echo "$var1"; } if(preg_match("/\[code\](.+?)\[\/code\]/",$_POST['post_form'],$post_array)){ $code = preg_replace("/\[code\](.+?)\[\/code\]/","<table width=\"400\" border=\"1\" bordercolor=\"black\" ><tr><td>Code:</td></tr><tr><td><font size=\"2\">$1</font></td></tr></table>",$_POST['post_form']); echo "$code"; } ?>
|
|
Ross
eCreations Staff
Posts: 1,768
|
PHP
Mar 28, 2007 18:57:22 GMT
Post by Ross on Mar 28, 2007 18:57:22 GMT
Looks better Probably best to ensure you filter out any HTML/Script tags to start with, especially if the content is going to be displayed to others. Just make the first replace one to change < and > to < and > Oh, are there's no line breaks
|
|
|
PHP
Mar 29, 2007 1:58:29 GMT
Post by FireIndy on Mar 29, 2007 1:58:29 GMT
Thanks Ross. Ive been thinking... I have a form with a div tag around it. So when the form submits, it submits and I tried to hide it using Javascript through PHP. So I tried setting my header to my index page and then doing the javascript(still same file):
if ($loggedin) { $fp = fopen('usersonlinedata.txt','a'); $content = "$user\n"; fwrite($fp,$content); fclose($fp); header("Location: index.php"); // I had the javascript here }
I tried the javascript up and below the header info and I knew the hiding part was right. Still no luck. I know I escaped all the quotes. But its been frustrating lol. So it goes:
login(if true)-->hide login-->index page
But I think the info isnt being sent to the headers. I dont know where to put this. I tried using include() in my index file so I could check if they were logged in, but the way the script works, there is text outputted on screen, so I had uneccessary text on my index.
The javascript was: echo '<script>var log = doument.getElementsById("login").style.display="none";</script>'
|
|