Odd dots in cookies

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
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Odd dots in cookies

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Odd dots in cookies

Post by requinix »

What's the code you have?
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post 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.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Odd dots in cookies

Post by mintedjo »

Sessions?
http://uk3.php.net/session
Thats whats usually done for keeping track of users
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Odd dots in cookies

Post by requinix »

Just a fun thing to check:

Change $uservar in testone.php to something else and see what happens.
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post by Teonnyn »

The actual variable or the name itself?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Odd dots in cookies

Post by requinix »

Teonnyn wrote:The actual variable or the name itself?
The contents. Like, change the username to something that's not "Teonnyn".
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post 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.";
?>
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Odd dots in cookies

Post 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";
 
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Odd dots in cookies

Post 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. :o
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Odd dots in cookies

Post by Syntac »

session_start() is confusingly named. That's what got me the first time.
Post Reply