Jump to content


Photo

$_GET and $_POST Question


  • Please log in to reply
8 replies to this topic

#1 Acidic.Saliva

Acidic.Saliva
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 July 2006 - 07:47 PM

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?

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 July 2006 - 07:55 PM

a session or a dynamic URL
Why doesn't anyone ever say hi, hey, or whad up world?

#3 Acidic.Saliva

Acidic.Saliva
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 July 2006 - 08:01 PM

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>"?

#4 JamesRyzon

JamesRyzon
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 02 July 2006 - 08:06 PM

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>"?


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

To set a session variable youll need to first

session_start();

on the page you want to use sessions on.

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

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

#5 Acidic.Saliva

Acidic.Saliva
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 July 2006 - 08:19 PM

aha, thanks!

#6 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 July 2006 - 08:38 PM

HI

This may help you out and addapt the code to suite

first page "LOGIN FORM PAGE"

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
{


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

session_start();
if($_SESSION['admin'] == "true"){
//ALL YOUR HEADER CODE

}

FOR LEVEL ONE AUTH

session_start();
if($_SESSION['level']=="limited1") {

//all your code
}
?>

FOR LEVEL TWO AUTH

session_start();
if($_SESSION['level1']=="limited2") {
//all your code
}
?>

FOR LEVEL THREE AUTH

session_start();
if($_SESSION['leve2']=="limited3") {
//all your code
}
?>



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
Im loving it ........

#7 Acidic.Saliva

Acidic.Saliva
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 July 2006 - 09:33 PM

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?

#8 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 July 2006 - 09:39 PM

Hi

Did you do session_start () on the other page?

on the next page to test it out

do

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

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

hope this helps
Im loving it ........

#9 Acidic.Saliva

Acidic.Saliva
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 July 2006 - 10:12 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users