why wont this work?
<?php
setcookie("beenhere", "yes", time() + 3600)
?>
<SOME HTML CODE HERE>
<?php
if ($_COOKIE["beenhere"] == "")
{
echo "Welcome to my website, hope you can stop by again";
}
else
{
echo "welcome back!";
}
?>
i have done this before and it worked... why doesnt it work now? am i making some kind of stupid mistake?i'm not real experienced in cookies or anything, but what's it doing? does it say "welcome back" no matter what, or does it say "Welcome to my website, hope you can stop by again" no matter what? or does it not say a thing? or do you get an error?You cannot access a cookie in the page it has been set (i.e. it requires a reload)!
PHP manual (function setcookie() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.setcookie.php">http://uk.php.net/manual/en/function.setcookie.php</a><!-- m -->)):Common Pitfalls: Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.transmothra- what the code does is decide whether the user has accessed my website before by reading a cookie. if the cookie is blank, the browser will display the welcome to my website message. if it is not blank (the cookie has been set to yes), then the browser will display "welcome back".
ok, torrent. so what you are saying is that i will need to put the setcookie function in a different page? i want to make sure though that on the first visit it will not display "Welcome back"... will it still work?Well, if it's the first visit no Cookie will be set and so it won't display welcome back.if (!isset($_COOKIE['beenhere'])) {
// first visit
} else {
// welcome back
}ah, thank you much torrent!ah, that didnt work, but for some reason this simple code decided to work!
<?php
setcookie("beenhere", "yes" , time() + 3600);
?>
<?php
if ($beenhere == "yes")
{
print "Welcome back!";
}
else
{
print "Hello";
}
?>
.... not working anymore... any other suggestions? for some reason that code worked on a blank website, but not when included in my actual website.morrow, yes that will work, but you have to refresh that page for the cookie to be read. as Torrent suggested, you have to reload that page for ANY cookie to be read or set in php.
if you run that then just refresh. other than that you have to load that cookie in another page and then forward to the page you want the cookie check on. as it sits now, the cookie will set everytime you load that page. not good practice that way.Also, if $beenthere worked then it means you have register_globals set to On which is bad. You really should change this in your php.ini file.
That also may cause you problems as your home server may have register_globals On, but your hosting server most likely has them Off, which would mean $beenthere would not work on it (you'd need to use $_COOKIE['beenthere']).well then is there a better way to do it then with cookies?
i ahve a MySQL DB, but im not sure how to use it... not sure if it would even connectyes we already told you. you have to have that setcookie in another page and refresh to the page you want. either way you do it the setcookie has to be in it's own page and not in the page that checks for the cookiei know that, but i was just asking if there was a beter way to do this than by using cookies. I have done a much more complex thing than this by storing names and locations and displaying them, but nothing sems to work here. I will try what you have suggested, but just one thing.
the refresh code is
<meta http-equiv.... and thats what i forgot after that, something about content refresh:30 or something.
anyway, thanks for the help.well the way you worded your last post it sounded liek you wanted to find a better way to use that cookie.
but in light of what you just posted, yes there are number of ways to do what you want. you can use sessions or you can use the db.
but depending on which way you go it will require some reading on each one. connecting to mysql is easy and I have a tutorial at my site explaining how to do it.thanks, can i have a link to your site?
anyway... i have this:
<?php
$abletoconnect = mysql_pconnect("localhost", "", "")
if ($abletoconnect != FALSE){
echo "A Persistent connection was made with the MySQL Database";
}
else
{
echo "Could not connect to the database"
}
?>
lemme see if it works...
okay, i was able to connect. I have a book here about creating tables and such, so ill take a look into this and see what i can figure out.
thanks for all the help.Originally posted by morrowasted
thanks, can i have a link to your site?
dont roll your eyes at me.
i clicked his website button and it came up "Page cannot be displayed.", so i didnt know if that was actually his. The reason i was confused is because last time Scoutt told me to check out his site it didnt take me to snippet library, so i figured it was that other one again.
either way, smiley-only posts are not allwoed (quotes dont count).you clicked on my little image in my sig? it works for me
<!-- w --><a class="postlink" href="http://www.snippetlibrary.comactually">www.snippetlibrary.comactually</a><!-- w -->, i clicked the website button in your post footer.
it works now.
i found this:
<?
mysql_connect ("localhost","username","password");
mysql_select_db ("database_name");
$sql = "select * from table";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$field1= $row["field1"];
$field2= $row["field2"];
echo "$field1<br>";
echo "$field2<p>";
}
?>
im really new to MySQL... how do I create a table? i have this admin thing, but Im not sure how to use ityou have phpmyadmin? then that will create a table.
I can't tell you how to do it as it is a lengthy process
read the manual for it } i do not have PHP my Admin. I have... lemme look for it (it came with MySQL)..."WinMySQLAdmin"...
<!-- m --><a class="postlink" href="http://24.206.105.78/mysqlpic.jpg">http://24.206.105.78/mysqlpic.jpg</a><!-- m -->
there are a list of tables there, but I do not see a way to create them.
the problem with my manual is, it shows some sort of DOS- style table creation program that I don't think I even have!it would be easier to downlaod phpMyadmin.PHPMyAdmin is an excellent and well worth learning as this is the tool most hositng company's provide you.
However, my own personal favourite was MySQLFront, but that has now been discontinued
You could try this one:
<!-- m --><a class="postlink" href="http://www.webyog.com/sqlyog/">http://www.webyog.com/sqlyog/</a><!-- m -->
It has a graphical user interface and receives very good reviews.thanks for the link, torrent.
I have Download ed the program. On load, it asks...
MySQL Host Address. I put "localhost".
It asks username and password... this was where i started to get confused. I found the my.ini setup and it shows the following:
#This File was made using the WinMySQLAdmin 1.4 Tool
#5/26/2003 11:58:21 AM
#Uncomment or Add only the keys that you know how works.
#Read the MySQL Manual for instructions
[mysqld]
basedir=C:/mysql
#bind-address=24.206.105.78
datadir=C:/mysql/data
#language=C:/mysql/share/your language directory
#slow query log#=
#tmpdir#=
#port=3306
#set-variable=key_buffer=16M
[WinMySQLadmin]
Server=C:/mysql/bin/mysqld-nt.exe
user=morrowasted
password=wow
QueryInterval=10
now, in there are my username and password (i changed the password here for security purposes).
It also asks for my databases... i opened the databases tag, and two are listed: mysql and test. I put mysql, because there are no tables in test.
The problem: when i click "connect" it says...
Err. #1045 Access Denied for user: "[email protected]" Using Password(YES)
any ideas?The MySQL database it refers to is actually a special database that requires root access. It's not the database you will work with.
It sounds as though you have created a database called Test. Use that one and start adding your tables as and when you need them.well, it still isn't working. i found this list of options, but i am not sure wha it means:
<!-- m --><a class="postlink" href="http://24.206.105.78/mysql2pic.jpgaha">http://24.206.105.78/mysql2pic.jpgaha</a><!-- m -->! strange... by simply leaving the defaults (no password, username:root, localhost) with the DB test it worked. cool.
now i just need to figure this thing out...
okay, its open now.. i made a new database, d2i (for my website in progress, d2i, because test just didnt seem very accurate )
anyway, none of the menu shortcuts seem to be working, but there is a manual SQL editor here, lemme see...
er... well, when i click enter it just goes to the next line, nothing happens.
heres what it says:
Database Information For - test
----------------------------------
Name Type Row_Format Rows ETCETCETC
---- ---- --------- ----
theres a bunch of those....
wait a second... why does it say for test, if i ust selected d2i from the dropdown box of servers?
oh, fine,i'll use test!
wow i just think i figured it out.
I found a table editor here, similar to MS Access. I suppose I will try the 'name' and 'password' thing. hmm... which shall be the key? ah, name'll work i guess.
double is letters and numbers right? i hope so...
uh oh!
<!-- m --><a class="postlink" href="http://24.206.20.78/sqlyogerr.jpg">http://24.206.20.78/sqlyogerr.jpg</a><!-- m -->
<?php
setcookie("beenhere", "yes", time() + 3600)
?>
<SOME HTML CODE HERE>
<?php
if ($_COOKIE["beenhere"] == "")
{
echo "Welcome to my website, hope you can stop by again";
}
else
{
echo "welcome back!";
}
?>
i have done this before and it worked... why doesnt it work now? am i making some kind of stupid mistake?i'm not real experienced in cookies or anything, but what's it doing? does it say "welcome back" no matter what, or does it say "Welcome to my website, hope you can stop by again" no matter what? or does it not say a thing? or do you get an error?You cannot access a cookie in the page it has been set (i.e. it requires a reload)!
PHP manual (function setcookie() (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/function.setcookie.php">http://uk.php.net/manual/en/function.setcookie.php</a><!-- m -->)):Common Pitfalls: Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.transmothra- what the code does is decide whether the user has accessed my website before by reading a cookie. if the cookie is blank, the browser will display the welcome to my website message. if it is not blank (the cookie has been set to yes), then the browser will display "welcome back".
ok, torrent. so what you are saying is that i will need to put the setcookie function in a different page? i want to make sure though that on the first visit it will not display "Welcome back"... will it still work?Well, if it's the first visit no Cookie will be set and so it won't display welcome back.if (!isset($_COOKIE['beenhere'])) {
// first visit
} else {
// welcome back
}ah, thank you much torrent!ah, that didnt work, but for some reason this simple code decided to work!
<?php
setcookie("beenhere", "yes" , time() + 3600);
?>
<?php
if ($beenhere == "yes")
{
print "Welcome back!";
}
else
{
print "Hello";
}
?>
.... not working anymore... any other suggestions? for some reason that code worked on a blank website, but not when included in my actual website.morrow, yes that will work, but you have to refresh that page for the cookie to be read. as Torrent suggested, you have to reload that page for ANY cookie to be read or set in php.
if you run that then just refresh. other than that you have to load that cookie in another page and then forward to the page you want the cookie check on. as it sits now, the cookie will set everytime you load that page. not good practice that way.Also, if $beenthere worked then it means you have register_globals set to On which is bad. You really should change this in your php.ini file.
That also may cause you problems as your home server may have register_globals On, but your hosting server most likely has them Off, which would mean $beenthere would not work on it (you'd need to use $_COOKIE['beenthere']).well then is there a better way to do it then with cookies?
i ahve a MySQL DB, but im not sure how to use it... not sure if it would even connectyes we already told you. you have to have that setcookie in another page and refresh to the page you want. either way you do it the setcookie has to be in it's own page and not in the page that checks for the cookiei know that, but i was just asking if there was a beter way to do this than by using cookies. I have done a much more complex thing than this by storing names and locations and displaying them, but nothing sems to work here. I will try what you have suggested, but just one thing.
the refresh code is
<meta http-equiv.... and thats what i forgot after that, something about content refresh:30 or something.
anyway, thanks for the help.well the way you worded your last post it sounded liek you wanted to find a better way to use that cookie.
but in light of what you just posted, yes there are number of ways to do what you want. you can use sessions or you can use the db.
but depending on which way you go it will require some reading on each one. connecting to mysql is easy and I have a tutorial at my site explaining how to do it.thanks, can i have a link to your site?
anyway... i have this:
<?php
$abletoconnect = mysql_pconnect("localhost", "", "")
if ($abletoconnect != FALSE){
echo "A Persistent connection was made with the MySQL Database";
}
else
{
echo "Could not connect to the database"
}
?>
lemme see if it works...
okay, i was able to connect. I have a book here about creating tables and such, so ill take a look into this and see what i can figure out.
thanks for all the help.Originally posted by morrowasted
thanks, can i have a link to your site?
dont roll your eyes at me.
i clicked his website button and it came up "Page cannot be displayed.", so i didnt know if that was actually his. The reason i was confused is because last time Scoutt told me to check out his site it didnt take me to snippet library, so i figured it was that other one again.
either way, smiley-only posts are not allwoed (quotes dont count).you clicked on my little image in my sig? it works for me
<!-- w --><a class="postlink" href="http://www.snippetlibrary.comactually">www.snippetlibrary.comactually</a><!-- w -->, i clicked the website button in your post footer.
it works now.
i found this:
<?
mysql_connect ("localhost","username","password");
mysql_select_db ("database_name");
$sql = "select * from table";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$field1= $row["field1"];
$field2= $row["field2"];
echo "$field1<br>";
echo "$field2<p>";
}
?>
im really new to MySQL... how do I create a table? i have this admin thing, but Im not sure how to use ityou have phpmyadmin? then that will create a table.
I can't tell you how to do it as it is a lengthy process
read the manual for it } i do not have PHP my Admin. I have... lemme look for it (it came with MySQL)..."WinMySQLAdmin"...
<!-- m --><a class="postlink" href="http://24.206.105.78/mysqlpic.jpg">http://24.206.105.78/mysqlpic.jpg</a><!-- m -->
there are a list of tables there, but I do not see a way to create them.
the problem with my manual is, it shows some sort of DOS- style table creation program that I don't think I even have!it would be easier to downlaod phpMyadmin.PHPMyAdmin is an excellent and well worth learning as this is the tool most hositng company's provide you.
However, my own personal favourite was MySQLFront, but that has now been discontinued
You could try this one:
<!-- m --><a class="postlink" href="http://www.webyog.com/sqlyog/">http://www.webyog.com/sqlyog/</a><!-- m -->
It has a graphical user interface and receives very good reviews.thanks for the link, torrent.
I have Download ed the program. On load, it asks...
MySQL Host Address. I put "localhost".
It asks username and password... this was where i started to get confused. I found the my.ini setup and it shows the following:
#This File was made using the WinMySQLAdmin 1.4 Tool
#5/26/2003 11:58:21 AM
#Uncomment or Add only the keys that you know how works.
#Read the MySQL Manual for instructions
[mysqld]
basedir=C:/mysql
#bind-address=24.206.105.78
datadir=C:/mysql/data
#language=C:/mysql/share/your language directory
#slow query log#=
#tmpdir#=
#port=3306
#set-variable=key_buffer=16M
[WinMySQLadmin]
Server=C:/mysql/bin/mysqld-nt.exe
user=morrowasted
password=wow
QueryInterval=10
now, in there are my username and password (i changed the password here for security purposes).
It also asks for my databases... i opened the databases tag, and two are listed: mysql and test. I put mysql, because there are no tables in test.
The problem: when i click "connect" it says...
Err. #1045 Access Denied for user: "[email protected]" Using Password(YES)
any ideas?The MySQL database it refers to is actually a special database that requires root access. It's not the database you will work with.
It sounds as though you have created a database called Test. Use that one and start adding your tables as and when you need them.well, it still isn't working. i found this list of options, but i am not sure wha it means:
<!-- m --><a class="postlink" href="http://24.206.105.78/mysql2pic.jpgaha">http://24.206.105.78/mysql2pic.jpgaha</a><!-- m -->! strange... by simply leaving the defaults (no password, username:root, localhost) with the DB test it worked. cool.
now i just need to figure this thing out...
okay, its open now.. i made a new database, d2i (for my website in progress, d2i, because test just didnt seem very accurate )
anyway, none of the menu shortcuts seem to be working, but there is a manual SQL editor here, lemme see...
er... well, when i click enter it just goes to the next line, nothing happens.
heres what it says:
Database Information For - test
----------------------------------
Name Type Row_Format Rows ETCETCETC
---- ---- --------- ----
theres a bunch of those....
wait a second... why does it say for test, if i ust selected d2i from the dropdown box of servers?
oh, fine,i'll use test!
wow i just think i figured it out.
I found a table editor here, similar to MS Access. I suppose I will try the 'name' and 'password' thing. hmm... which shall be the key? ah, name'll work i guess.
double is letters and numbers right? i hope so...
uh oh!
<!-- m --><a class="postlink" href="http://24.206.20.78/sqlyogerr.jpg">http://24.206.20.78/sqlyogerr.jpg</a><!-- m -->