How would I write to a different page? (My First Post!)
Moderator: General Moderators
-
technoreject
- Forum Newbie
- Posts: 6
- Joined: Fri Jul 01, 2011 8:53 pm
How would I write to a different page? (My First Post!)
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!)
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
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!)
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!)
Use sessions.
In page 1:
In page 2:
In page 1:
Code: Select all
session_start();
$_SESSION['food'] = 'pancakes';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!)
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
- 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!)
Don't be shy, try it!technoreject wrote:Does that require them to go to that page or will it automatically work?
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']++;technoreject wrote: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!)
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 owntechnoreject wrote:Does that require them to go to that page or will it automatically work?
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.Also how can I get it to stay there
Re: How would I write to a different page? (My First Post!)
Could someone post the recipe plz? I'd love some of those!flying_circus wrote:I'm not sure how you would auto-increment pancakes (though I wish I knew how, they are delicious!).
-
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!)
Also how can I get it to stay thereWhat 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!)
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.
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!)
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);
?>- 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!)
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!)
- 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.