Page 1 of 1
How would I write to a different page? (My First Post!)
Posted: Fri Jul 01, 2011 8:56 pm
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?

Re: How would I write to a different page? (My First Post!)
Posted: Fri Jul 01, 2011 9:35 pm
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
Re: How would I write to a different page? (My First Post!)
Posted: Fri Jul 01, 2011 9:39 pm
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?)
Re: How would I write to a different page? (My First Post!)
Posted: Sat Jul 02, 2011 2:54 am
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");
Re: How would I write to a different page? (My First Post!)
Posted: Sat Jul 02, 2011 10:52 pm
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?
Re: How would I write to a different page? (My First Post!)
Posted: Sun Jul 03, 2011 10:27 pm
by technoreject
Bump
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 1:39 am
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']++;
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 2:56 am
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.
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 2:58 am
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!

Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 3:23 pm
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?
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 4:14 pm
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.
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 10:36 pm
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);
?>
Re: How would I write to a different page? (My First Post!)
Posted: Mon Jul 04, 2011 11:52 pm
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());;
?>
Re: How would I write to a different page? (My First Post!)
Posted: Tue Jul 05, 2011 4:54 pm
by McInfo
- Salt and hash your passwords!
- Before you pass user-submitted data into the database, filter and sanitize it.
- 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.