News script

Had your computer crash on you, or a website shows wrong, or your printer went dead on you? Come on in...

Moderator: Crew

Post Reply
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

News script

Post by Chroelle »

Hiya (possibly only Zyx)

I have run into an issue. I really need a very simple way to add news on a frontpage (actually it is just keeping track of what is written on a simple frontpage) offline. I need to find something for a guy, where he can add the news to a page as he wants it to. No need to text-formatting or big arm-movements - he simply needs something taht can add text and remove text from his website. If it is done on a locked website it would be good, but if it needs to be done in code or in database then he is lost. We are talking about a man that did not feel comfortable using the word "link" and would prefer to tell me that when your mouse clicked something then you got to somewhere else. This should tell you what we are up against.
Something free - perhaps even using twitter and embedding, stuff like that. Any ideas? Anyone?
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Post by Zyx »

Well, one easy way to do that would be to have a bit of code on the front page that reads in a file like "news.txt" and displays the content of that file.

Then there's another script that handles editing that file, preferably secured with a password so not everyone can edit the news =)

If he doesn't feel comfortable with "link", I really doubt he'd be comfortable with twitter... =)
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

Ok, so the first solution you wrote - how would I go about doing that?
Should I make an embedding of the news.txt file you are talking about - is that what you mean?

If you have a link for soemthing, then please let me know! Or if you could talk me through it then that would be cool. :)
I would be very grateful for the latter, but understand if you cannot find the time.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

I found something about editable content in HTML5.
http://stackoverflow.com/questions/1766 ... on-the-fly

Any idea if I might be able to use that, and then when I embed it in his newspage - will it be editable then???
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Post by Zyx »

Chroelle wrote:Any idea if I might be able to use that, and then when I embed it in his newspage - will it be editable then???
Short answer is, no. Yes, you can edit content easily with that, but you still need the functionality to actually save your changes. Also, you need to secure that editing page.

I might have an example of how to do it soon.
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

That would be so very very cool! I would love to really dig into that and learn from it, as it is something I can really use for my webdesigns.
- And as you know - the money from the webdesigns is what I justify spending so much money on hosting for CWF with at home. :)
So if you could help me out with this I would be extremely grateful! This could earn me the entire hosting of CWF for a year, plus a little for advertising. :)
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Post by Zyx »

Okay, this is how you can achieve that thing.

1. Create a file called "news.txt" in the place where all the other files are. For example, write "Hello world" in the file. Make the file writable by Owner, Group and Others.

2. Open up the page that should show the news and make it look something like this:

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
$file = "news.txt";
$handle = fopen($file,"r");
$content = nl2br(fread($handle,filesize($file))); # nl2br, the minimal text processing.
fclose($handle);
?>

<h1>Front page</h1>

Hello!

<h2>News</h2>
<?=$content?>

</body>
</html>
The important bits are the PHP code that reads the contents of the file in and the <?=$content?> tag can be placed where you want the news to be shown at.

3. Open up the page in web browser, you should see "Hello world" or whatever you put in the news.txt file.

4. To edit the file, you need a script page like this, called "admin.php". (It doesn't matter what you name the file as long as you remember to change the form tag to match the filename)

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
$file = "news.txt";
if ($_POST["submit"]=="submit") {
    echo "Saving changes...";
    $content = stripslashes($_POST["text"]); # Surftown has magic_quotes_gpc on.    
    $handle = fopen($file,"w");
    if (!fwrite($handle,$content)) {
        echo "Error writing to file.<br>";
    } else {
        echo "ok<br>";
    }
    fclose($handle);
} else {
    $handle = fopen($file,"r");
    $content = fread($handle,filesize($file));
    fclose($handle);
}

?>

<h1>Edit stuff</h1>

<form action="admin.php" method="post">
<textarea name="text" rows="25" cols="80">
<?=$content?>
</textarea><br>
<button name="submit" type="submit" value="submit">Submit</button>
<button name="reset" type="reset">Reset</button>
</form>
</body>
</html>
5. Visit the admin.php and you should see the contents of the news.txt in there.

6. Change the text using admin.php and click submit. It should show that file saving was ok. The new text should now be visible in both admin.php and in the news page.

7. What's left is securing the admin.php so that only people who know a password can access it. This is managed in Apache web server quite easily with the use of .htaccess and .htpasswd files.

8. First create a .htpasswd file, for example here. I used user name "admin" and password "admin" to get this file:

Code: Select all

admin:$apr1$ug0L8...$EPmcRCHiwb8WV20WSrdif1
Add this file to the same directory with all the other files.

9. Lastly, we need to have a .htaccess file to tell Apache what to secure and how. If you named your admin.php file something else or used other username above, change them in the .htaccess file as well. Make sure that the AuthUserFile directive has the full path to the .htpasswd file, in Surftown's case it looks something like below.

Code: Select all

AuthType Basic
AuthName "Admin Site"
AuthUserFile /hsphere/local/home/chroelle/domain.dk/.htpasswd

<Files admin.php>
Require user admin
</Files>
10. Test that things work by going to admin.php, you should be greeted by browser's login box.

I'll put an example up later.
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Post by Chroelle »

Will look deeper into this. Need to read it over while having time to understand it, but thanks for the work put in to this. :)
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

Ok. I am really stuck at square one here. I have made a file called news.txt and made an entry in it. I have made a nyhed.htm which should show it - it doesn't. I have also tried renaming that to nyhed.php and it still wont work...

EDIT: Just figured out what I did wrong. It of coourse needed to be uploaded to show properly, as the php-code wasn't executed running from my machine.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

And stumbled into a new issue.... I cannot edit stuff in the admin.php file. I am not sure if I need to change the attributes of the file (read, write, execute).
I currently have 3 files:
admin.php as shown here: http://www.connect-webdesign.dk/Referen ... /admin.php
news.txt - which I filled in a sentence in, and that shows correctly in the admin-file.
Nyhed.php - which is the site that is going to show the news posted.

When I change things in the admin file and press submit it simply resets to whatever it was before without even giving me an error... I am baffled. Tried reading over teh code again and cannot seem to find anyhting wrong, so I hope you can. Haven't made htaccess file etc yet.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Re: News script

Post by Zyx »

make sure that the webserver can write to news.txt... but I tried to edit the text and it worked?
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

How do I make sure. Just making news.txt writable to user, group etc or...?
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Re: News script

Post by Zyx »

Chroelle wrote:How do I make sure. Just making news.txt writable to user, group etc or...?
yep, by making it readable/writable by all.
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

How about executable by all? I checked and everything was made readable and writable, and I still can't make any changes that lasts longer than when I press submit... Can it be browser-related? I am using IE8... (Just checked and it works with Firefox...:))

Ok, so back to the drawingboard. Why doesn't this work with IE8? (There is no way I can make this customer change to Firefox. He is propably surfing the internet with Netscape or something even older...)
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Re: News script

Post by Zyx »

Chroelle wrote:How about executable by all?
That doesn't have any effect on anything else than executable scripts. (pl, cgi, etc.)
I checked and everything was made readable and writable, and I still can't make any changes that lasts longer than when I press submit... Can it be browser-related? I am using IE8... (Just checked and it works with Firefox...:))
I think you answered your own question.
Ok, so back to the drawingboard. Why doesn't this work with IE8? (There is no way I can make this customer change to Firefox. He is propably surfing the internet with Netscape or something even older...)
It has to be something how the form is submitted, maybe a parameter or something is missing. Hmm...
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

Let me know if you figure it out because I am at a loss until then. I am trying to learn this stuff as we go along so I am learning a lot from what you are helping me with. Also if this works, then perhaps I already have another customer that can use this particular script, so this would help connect-webdesign and CWF quite a lot regarding funding of the server. :) (I really do appreciate this...)
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Zyx
Pretender to the throne
Posts: 1907
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki
Contact:

Re: News script

Post by Zyx »

Fixed!

I changed the line that checks if the form was submitted to

Code: Select all

if ($_POST["submit"] && strtolower($_POST["submit"])=="submit") {
because for some reason IE stores the Submit-button's value as "Submit" instead of "submit". Now that check is case insensitive.
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

VERY COOL! Will try it out when I come home this evening.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

Ok I made the ultimate sin and tried messing with this while tired. Got editing of text working, but cannot seem to get it to work so that it is inserted where I want it to. I am however sure that has more to do with my tired eyes than your code. :)
I will look into this again in the weekend.

You wrote some time ago that you would put up an example later... Did you have one? I would love to see one, but if you dont have one ready then dont bother. It might not be worth the work.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
User avatar
Chroelle
Admin emeritus
Posts: 9870
Joined: Fri Feb 17, 2006 9:19
Location: Location, location...
Contact:

Re: News script

Post by Chroelle »

I actually succeded in making this work now. Amazing after 4 hours of cleaning the new apartement today, and having the kiddo on the side with hysteria because she is bored stiff when we are cleaning...
Thanks for all the work Zyx. This should now work perfectly for me and the customer.
Currently testing Life version 2.9 (With added second child)
(Beta testing in progress)

www.paed-it.dk - My blog in Danish

Clothes make the man. Naked people have little or no influence on society.
--Mark Twain
Post Reply