$_GET question

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
Colwell
Forum Newbie
Posts: 6
Joined: Fri Nov 06, 2009 2:12 pm

$_GET question

Post by Colwell »

I have a form that you complete
<form action="welcome.php" method="get">
Username<input type="text" name="username" />
Password<input type="text" name="pass" />
Email<input type="text" name="email" />
<input type="Submit" />
Then it passes all this stuff to the next page where it is put into a sentence, (the other stuff is put into the database)
<?php $user=$_GET["username"];
echo "Welcome ",$user,". You have been logged in.";
?>
<a href="http://colwell.uuuq.com/home.php">Continue</a>
Now I was wondering, how can I put the $user variable into the address of home.php so that I can $_GET it again.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: $_GET question

Post by VladSun »

Put in in the query string for home.php anchor: home.php?name=$NAME
Or you could store it into the $_SESSION variable which I think is better
There are 10 types of people in this world, those who understand binary and those who don't
Colwell
Forum Newbie
Posts: 6
Joined: Fri Nov 06, 2009 2:12 pm

Re: $_GET question

Post by Colwell »

Ok, I am new to this so, I tried
<a href="http://colwell.uuuq.com/home.php?username=<?php echo $user;?>">Continue</a>
and I didn't come close. I'm guessing your link is inside the <?php ?> brackets?

Anyways, could you briefly explain how to use the $_SESSION method?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: $_GET question

Post by VladSun »

Colwell wrote:Ok, I am new to this so, I tried
<a href="http://colwell.uuuq.com/home.php?username=<?php echo $user;?>">Continue</a>
and I didn't come close.
It's exaclty how it should be done ... I don't know what went wrong ...
Colwell wrote:Anyways, could you briefly explain how to use the $_SESSION method?
Hm.... I thought you are familiar with working with sessions - it seems you are building a login page.

Take a look at the examples in the manual:
http://php.net/manual/en/function.session-start.php
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: $_GET question

Post by flying_circus »

VladSun wrote:
Colwell wrote:Ok, I am new to this so, I tried
<a href="http://colwell.uuuq.com/home.php?username=<?php echo $user;?>">Continue</a>
and I didn't come close.
It's exaclty how it should be done ... I don't know what went wrong ...
Perhaps a PHP Notice because $_GET['username'] was not set?
Colwell
Forum Newbie
Posts: 6
Joined: Fri Nov 06, 2009 2:12 pm

Re: $_GET question

Post by Colwell »

OK, so I have
session_start();
In the header and
$user=$_GET["username"];
$_SESSION['username']=$user;
on the page where I already have the information to $_GET in the address bar.Then on the next page I have
echo $_SESSION['username'];
and I don't have anything displayed, is this just a syntax error?
Colwell
Forum Newbie
Posts: 6
Joined: Fri Nov 06, 2009 2:12 pm

Re: $_GET question

Post by Colwell »

This is really strange actually, I'm not sure if my header is working. It does but it doesn't. It contains
<?php
session_start();
?>
<html>
<body>
<?php //connection junk:(
$con = mysql_connect("localhost:3306","colwell_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("colwell_db", $con);
//connection junk:)
echo "The header is here!!";
?>
and it connects to the database and identifies the code as html but won't display the echoed bit and I don't think it is starting the session. I include it on the required pages using
include("header.php");
Last edited by Colwell on Sun Nov 08, 2009 3:44 pm, edited 1 time in total.
Daniel03155
Forum Newbie
Posts: 3
Joined: Sat Nov 07, 2009 9:06 pm

Re: $_GET question

Post by Daniel03155 »

Hello,

Code: Select all

session_start();
/* Html stuff */
$connection = mysql_connect("localhost", "user", "pass") or die(mysql_error());
$select_db = mysql_select_db("Database") or die(mysql_error()); 
echo "Works.";
Btw the second argument to mysql_select_db is optional. The code above works perfectly, it's basically the same as yours though a little cleaner. You can check if the session started or not by going to your browser's options and checking the cookies.(in FF's case is "remove individual cookies"). Search for localhost.
Post Reply