Page 1 of 1
Odd dots in cookies
Posted: Tue Dec 23, 2008 4:16 am
by Teonnyn
Hello to the forums! This is my first post here and I'm rather confused about something regarding cookies.
I'm just learning how to use them and they seem to be the best choice to transfer data between two PHP
pages. I've gotten the initial transfer working with "testone.php" setting up a cookie and defining a username
variable. "testtwo.php" reads the cookie, and outputs it as a echo - the output is this:
This is a test, your username .TEONNYN. should be all caps.
I can't figure out where those two dots are coming from, or how to get rid of them.
This is the output from testone.php: Your username on this page is Teonnyn
- no dots. Does anyone have any advice on removing those two dots, or on better methods of passing variables without using forms?
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 4:32 am
by requinix
What's the code you have?
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 4:37 am
by Teonnyn
testone.php
Code: Select all
<?php
$uservar = "Teonnyn";
echo "Your username on this page is $uservar";
setcookie("user","$uservar");
?>
testtwo.php
Code: Select all
<?php
$test = ($_COOKIE["user"]);
$upper = strtoupper($test);
echo "This is a test, your username $upper should be all caps.";
if (($_COOKIE["user"]) == "Teonnyn") {
echo "Great! Your name is Teonnyn!";
}
?>
The whole "upper" thing was just a test to see if I could manipulate the string. The if statement was added in to see if I could catch where the dots were being added, so far it seems to be somewhere in the transfer.
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 4:39 am
by mintedjo
Sessions?
http://uk3.php.net/session
Thats whats usually done for keeping track of users
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 4:43 am
by Teonnyn
I tried playing around with sessions but never got the examples working on two different pages like the cookies seem to be - material used: Beginning PHP 5 and mySQL, and w3Schools. My overall goal is to develop a small script that'll transfer a username from Joomla into a Flash-based program to check if the user is logged in.
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 5:03 am
by requinix
Just a fun thing to check:
Change $uservar in testone.php to something else and see what happens.
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 5:13 am
by Teonnyn
The actual variable or the name itself?
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 5:18 am
by requinix
Teonnyn wrote:The actual variable or the name itself?
The contents. Like, change the username to something that's not "Teonnyn".
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 5:34 am
by Teonnyn
Interesting.. I changed it to "Extratest" and removed the upper and if code. Testone now outputs "Your username on this page is Extratest", while TestTwo doesn't seem to be getting it at all - "This is a test, your username should be ."
testtwo.php
Code: Select all
<?php
$test = ($_COOKIE["user"]);
echo "This is a test, your username should be .$test.";
?>
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 5:41 am
by Teonnyn
Is there a really good tutorial on using sessions? I haven't managed to find one yet and my book only explains the various parts, but doesn't go in depth on how to use them. Plus - the one thing I really need to know about sessions is this: Can they be used between two completely independent scripts on the same server as long as they both reference the same session? Independent meaning no form or html hyperlink between pages.
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 9:54 am
by mattpointblank
Teonnyn wrote:Interesting.. I changed it to "Extratest" and removed the upper and if code. Testone now outputs "Your username on this page is Extratest", while TestTwo doesn't seem to be getting it at all - "This is a test, your username should be ."
testtwo.php
Code: Select all
<?php
$test = ($_COOKIE["user"]);
echo "This is a test, your username should be .$test.";
?>
You don't need the dots surrounding the variable when used in that context. You only use dots outside of quote marks (see below) - anything inside quotes in an echo statement will be printed.
Code: Select all
$variable = "string":
echo "This is a sample concatenation " . $variable . " in this space";
// outputs 'This is a sample concatenation string in this space'
// equivalent to
echo "This is a sample concatenation $variable in this space";
As for sessions, they'll work across multiple pages on the same server, so long as the user keeps their browser open. Something like:
file1.php
Code: Select all
session_start();
$_SESSION['yourName'] == "Matt";
file2.php
Code: Select all
session_start();
$name = $_SESSION['yourName'];
echo "Hello, $name";
Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 11:53 am
by Teonnyn
Do'oh! That would explain why I had no luck getting sessions to work. I thought only one session start was needed. Anyway, thank you all! Thus far this has been one of the most responsive forums I've been in ever.

Re: Odd dots in cookies
Posted: Tue Dec 23, 2008 9:56 pm
by Syntac
session_start() is confusingly named. That's what got me the first time.