How would I write to a different page? (My First Post!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

How would I write to a different page? (My First Post!)

Post by technoreject »

I want (instead of using MySQL) to have my website write to another hidden page, then read from that page to get variables etc, how would I go about doing this? :)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How would I write to a different page? (My First Post!)

Post by twinedev »

Use serialize to get the variables over to a string that can be stored
http://php.net/serialize

Use fopen/fwrite to write them out
http://php.net/fopen
http://php.net/fwrite

Then once you have them saved, use file-get-contents to read that back in
http://php.net/file-get-contents

Lastly, use unserialize to assign that contents back to a variable.
http://php.net/unserialize

-Greg
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

Re: How would I write to a different page? (My First Post!)

Post by technoreject »

It won't let me write to my other page. Is there a way I can set my page to allow writing to it? (possibly with a password?)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How would I write to a different page? (My First Post!)

Post by Apollo »

Use sessions.

In page 1:

Code: Select all

session_start();
$_SESSION['food'] = 'pancakes';
In page 2:

Code: Select all

session_start();
$s = $_SESSION['food'];
print("I prefer to eat $s today");
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

Re: How would I write to a different page? (My First Post!)

Post by technoreject »

Does that require them to go to that page or will it automatically work? Also how can I get it to stay there and somewhat auto-increment when another one gets added?
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

Re: How would I write to a different page? (My First Post!)

Post by technoreject »

Bump
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: How would I write to a different page? (My First Post!)

Post by flying_circus »

technoreject wrote:Does that require them to go to that page or will it automatically work?
Don't be shy, try it!

technoreject wrote:Also how can I get it to stay there and somewhat auto-increment when another one gets added?
I'm not sure how you would auto-increment pancakes (though I wish I knew how, they are delicious!). If instead you were to store a number as a session variable, then sure, you can auto-increment. On each page go something like: $_SESSION['some_id']++;
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How would I write to a different page? (My First Post!)

Post by Apollo »

technoreject wrote:Does that require them to go to that page or will it automatically work?
What would automatically works? If nobody visits any page, nothing will happen. The php script is executed when someone (or a cron job) visits it. It won't magically do anything on its own :)
Also how can I get it to stay there
What do you mean with 'stay there'? Sessions are bound to individual visitors, and they're typically 'temporary', e.g. gone after they close their browser or stay away too long or something (depends on settings). For a more permanent storage you could use non-expiring cookies, but they can still be deleted by the visitor of course, plus you're now storing data client-side (as opposed to session data which is safely stored server-side). An alternative would be to store stuff in a database and save only a unique ID in a cookie.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How would I write to a different page? (My First Post!)

Post by Apollo »

flying_circus wrote:I'm not sure how you would auto-increment pancakes (though I wish I knew how, they are delicious!).
Could someone post the recipe plz? I'd love some of those! Image
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

Re: How would I write to a different page? (My First Post!)

Post by technoreject »

Also how can I get it to stay there
What do you mean with 'stay there'? Sessions are bound to individual visitors, and they're typically 'temporary', e.g. gone after they close their browser or stay away too long or something (depends on settings). For a more permanent storage you could use non-expiring cookies, but they can still be deleted by the visitor of course, plus you're now storing data client-side (as opposed to session data which is safely stored server-side). An alternative would be to store stuff in a database and save only a unique ID in a cookie.

I mean won't I have to echo out every time to be able to make a database type thing? So the other things won't get deleted from other people?
Like, if I went to my website and made a username named "technoreject", in my database page I would want it to be like "username1 = technoreject"

Then another person joins, his username is "Bob", in my database page technoreject will still be there (it should always be there now until I (the website owner) delete it), and it would write another username "username2 = Bob" and so on, so technoreject and bob will never get deleted.

My problem is when someone goes to that page I don't know how to get the page to remember what was written before, and I can't echo out what it doesn't remember.

Is there no way to do this?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How would I write to a different page? (My First Post!)

Post by Apollo »

My advice: read some introduction or tutorial on SQL and how to use it in PHP. These things are really very basic, you'll really benefit from getting some core knowledge about this.

To answer your question: yes, what you ask is possibly of course, just create a table in your database with at least one field called 'username' or something (and perhaps other fields like 'email', 'age', 'favorite_food', etc). Then you can store information about multiple users there. No need to echo anything. Echo'ing or printing is to generate html output (i.e. for a visitor to see), this is unrelated to storing stuff in a database.
technoreject
Forum Newbie
Posts: 6
Joined: Fri Jul 01, 2011 8:53 pm

Re: How would I write to a different page? (My First Post!)

Post by technoreject »

I've decided to use a MySQL database, I was creating my site, and as a test I wanted to update username123 with the password MYnewPASSWORD, no errors get ran but it doesn't update.

Code: Select all

<?php

$dbc = mysql_connect('[MyHost].000webhost.com','[MyUsername]','[MyPassword]');
$dbs = mysql_select_db([MyDatabase]',$dbc);

$query = "UPDATE logins SET pass=lol WHERE user=username123";
$result = mysql_query($query);

?>
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: How would I write to a different page? (My First Post!)

Post by phazorRise »

Code: Select all

<?php

$dbc = mysql_connect('[MyHost].000webhost.com','[MyUsername]','[MyPassword]');
if(!$dbc)
die('can not connect'.mysql_error());
 
$dbs = mysql_select_db([MyDatabase]',$dbc);
if(!$dbs)
die('can not select db'.mysql_error());

$query = "UPDATE logins SET pass='lol' WHERE user='username123'";
$result = mysql_query($query) or die('query error'.mysql_error());;

?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How would I write to a different page? (My First Post!)

Post by McInfo »

  1. Salt and hash your passwords!
  2. Before you pass user-submitted data into the database, filter and sanitize it.
  3. Before you output strings, ensure they are encoded in a way that will not conflict with the surrounding markup.
If you don't understand those concepts, please do everyone a favor and learn about them before you proceed.
Post Reply