Jump to content

Archived

This topic is now archived and is closed to further replies.

Acidic.Saliva

$_GET and $_POST Question

Recommended Posts

I know how to handle $_GET and $_POST with HTML forms, but say someone logs in on my site and it shows their name as a greeting "Hello (username here)!", but I also want it to show up on all the other pages as long as they're logged in. How can I keep sending the information to the new page when they click a link?

Share this post


Link to post
Share on other sites
lol, I'm kind of a noob, how would I do either of those?

EDIT: Oh wait, could I just say like echo "<a href="myphppage.php?username=$username>"?

Share this post


Link to post
Share on other sites
[quote author=Acidic.Saliva link=topic=99222.msg390688#msg390688 date=1151870475]
lol, I'm kind of a noob, how would I do either of those?

EDIT: Oh wait, could I just say like echo "<a href="myphppage.php?username=$username>"?
[/quote]

You could do that but then in theory if you arent Securing it anyone could just type into their bar http://path.to/yoursite.php?username=whatever

To set a session variable youll need to first

[code] session_start(); [/code]

on the page you want to use sessions on.

Then its as simple as
[code]
$_SESSION['username'] = $someformofusername;
[/code]

and calling $_SESSION['username'] in your code later

Share this post


Link to post
Share on other sites
HI

This may help you out and addapt the code to suite

first page "LOGIN FORM PAGE"
[quote]session_start ();
include ('includes/dbconfig.php') ;

// if logout == true
if($_GET['Logout'] == "true"){
unset($_SESSION);
session_destroy();
header('Location: login.php');
}

if (isset($_POST['btn_login'])){
mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname) or die ( mysql_error ());
$sql="SELECT * FROM users WHERE username = '".$_POST['txt_username']."' AND password = '".md5($_POST['txt_password'])."'";
$result=mysql_query($sql) or die (mysql_error() );

if(mysql_num_rows($result) > 0){
$row=mysql_fetch_assoc($result) or die (mysql_error() );//GET INFORMATION FROM DB ASSING TO SESSION
$_SESSION['level']=$row['level'];
$_SESSION['real_name']=$row['real_name'];
}
elseif ($_POST['txt_username'] =="" or $_POST['txt_password'] ==""){
  $input_error =  '<p align="center" class=style8>No Username or Password Entered!</p>' ;//nothing in pass or user box

}
else
{
  $input_error =  '<p align="center" class=style8>No such Username or Password Found!</p>' ;// no user in db

}
}
//WHAT EVER LEVEL RECIVED FROM DB IN ASSOC WITH USERNAME ASSIGN SESSION HERE AND RE-DIRECT USER
if($_SESSION['level']=="admin")
{
$_SESSION['admin']="true" ;
header('Location: admin_header.php');
}
elseif($_SESSION['level']=="limited1")
{
$_SESSION['level1']="true" ;
header('Location: user_header.php');
}
elseif($_SESSION['level']=="limited2")
{
$_SESSION['level2']="true" ;
header('Location: user_header2.php');
}
elseif($_SESSION['level']=="limited3")
{
$_SESSION['level3']="true" ;
header('Location: user_header3.php');
}
else//IF SESSION == FALSE ECHO FORM BELLOW AGAIN
{ [/quote]

that code will allow 4 different users to be redirected to differrent headers deppendent on name in level coloum in database
in the last else put your html code Login form code to be echoed
with this example you will need to create 4 different headers to be redirected to

ok in all the other pages at the top you will need to put
THIS ONE IS FOR ADMIN
[quote]
session_start();
if($_SESSION['admin'] == "true"){
//ALL YOUR HEADER CODE

}[/quote]
FOR LEVEL ONE AUTH
[quote]session_start();
if($_SESSION['level']=="limited1") {

//all your code
}
?>[/quote]
FOR LEVEL TWO AUTH
[quote]session_start();
if($_SESSION['level1']=="limited2") {
//all your code
}
?>[/quote]
FOR LEVEL THREE AUTH
[quote]session_start();
if($_SESSION['leve2']=="limited3") {
//all your code
}
?>[/quote]


now if you create a logout button on your page use direct in the url login.php?logout=true
(login.php)is the page name the main logout code is in and the password and username html text boxes are
but you can adapt this anyway you would like this is one i wrote for a stores database and still doing the pages for it 

hope this helps you out

Share this post


Link to post
Share on other sites
Hmm... I did just like you said,

session_start() before everything and I stored the username. It works fine if I call $_SESSION['username'] on the same page, but when I move the the linked page, it doesn't work. No error message, just doesn't print it out. Is there something I'm supposed to do to enable it on the other page?

Share this post


Link to post
Share on other sites
Hi

Did you do session_start () on the other page?

on the next page to test it out

do

[code]session_start() ;
echo $_SESSION['name'];[/code]

you should then have what ever that session is holding in the next page.

hope this helps

Share this post


Link to post
Share on other sites
Yep! That Did It! Thanks! I thought that if you did session_start() again, it would restart it and wipe everything out, but I guess I was wrong.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.