Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new to sessions.. think im confused.. 1

Status
Not open for further replies.

unborn

Programmer
Joined
Jun 26, 2002
Messages
362
Location
US
Here is the Top code:

<?
session_name(bgselect);
session_start();
session_unregister(bgpic);

if ($bgpic == &quot;corey&quot; || !isset($bgpic)) $bgpicture = &quot;dismain1.jpg&quot;;
if ($bgpic == &quot;spike&quot;) $bgpicture = &quot;dismain2.jpg&quot;;
if ($bgpic == &quot;clown&quot;) $bgpicture = &quot;dismain3.jpg&quot;;
if ($bgpic != &quot;corey&quot; || $bgpic != &quot;spike&quot; || $bgpic != &quot;clown&quot;) $bgpicture = &quot;dismain1.jpg&quot;;
echo $bgpic;
session_register(bgpic);

?>




Here is the body tag:
<body background=&quot;<? echo $bgpicture; ?>&quot;>

here is example link code:
<a href=&quot;switch.php?bgpic=corey&quot; target=&quot;_self&quot;>switch</a>


i finally got it working so when i push buttons it changed the bg pic finally... now.. i added sessions.. and im not quite sure how they work.. i got them workign with my news script.. but i didnt really have to change value in a session... can you change a value of a session string once its registered? if not do i unregister it then re register? or can i just register it again with out unregistering? i dunno any help would be apprecaited.


thanks a bunch!

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
btw... sence i added sessions in.. the bg wont even change.. i think it registered it empty and it wont reregister as anyhtign else :/


btw purpose of this is to make it so once they chose the bg it will stay that way when they return and what not my layout is one way but you can change the side pic to 3 diff pics..




if ya need refrence (its the 3 squares on the left that changfe the pics)


thanks again

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
1. You set a custom session name - it should be a string but you don't quote it. PHP looks for a CONSTANT by that name and since it doesn't find one reverts to the string value. You really don't have to give the session a name, PHP default setting will work fine.

2. Assign variables into the superglobal $_SESSION array rather than using session_register. It is clearer and will work regardless of register_global settings.$_SESSION['background'] = $myValue;

3. You can manipulate the session variables by assigning different values the way described in #2.

4. Retrieve the variables from $_SESSION['parameterName']. Since $_SESSION is superglobal you need not globalize the vars, they are available anywhere in the scope.

Ok?
 
ok so i would


sessions_start();


then i would register the value thing .. bleh so confused :/


ok see in my link how i use &quot;bgpic&quot; in the link... that will change the value of bgpic ... now sence it changes it... and then reloads the page.. should i register it right after start.. then it will catch that bgpic has changed and then reregister bgpic as the value i gave it? or should i use a diffrent variable... and then have it register a strign depending on the variable?

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
What you have to do if there is a change via some parameter passed to the script is:

1. Check for a new value. You use a GET parameter. In this case it would be:
Code:
# at the beginning
session_start();

# check if the bgpic changed
if (isset($_GET['bgpic']) AND $_SESSION['bgpic'] != $_GET['bgpic']){
   $_SESSION['bgpic'] = $_GET['bgpic'];
}
In the entire code you should refer to the current bgpic valus as $_SESSION['bgpic'];

The session remembers your values on the server. You want to use those values throughout the scripts, because when you do session_start() these values are available.
When you change it through the interface (link, form etc.) you need to update the stored values to the selected ones.
 
ooh ok im catching on now... see i read some book and they were all talkin about just use $string from gettign session info.. but now i see deffinately why you would wanna use $SESSION and $GET ... now would i use $GET to get info i $POST ? or do i match it with what i used?



btw thanks for all your help i think youve helped out in every post ive made. learning php is fun hehe :D thanks again!

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
also woudl this work?



$sbgpic = $_SESSION['bgpic']
$gbgpic = $_GET['bgpic']

and then change them by like

$sbgpic = $value

or should i do the full big thign the whole time?


(im a neat freak hehe and the big thign just looks sooo ugly hehehe :D)



in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
still not working :/ this is what i have.

Code:
<? 
	session_start();
	$gbgpic = $_GET['bgpic'];
	$sbgpic = $_SESSION['bgpic'];
	if (isset($gbgpic) AND $sbgpic != $gbgpic)
	{
	   $_SESSION['bgpic'] = $_GET['bgpic'];
	}
	else
	{
		$sbgpic = &quot;corey&quot;;
	}
	
		if ($sbgpic == &quot;corey&quot; || !isset($sbgpic)) $bgpicture = &quot;dismain1.jpg&quot;;
		if ($sbgpic == &quot;spike&quot;) $bgpicture = &quot;dismain2.jpg&quot;;
		if ($sbgpic == &quot;clown&quot;)  $bgpicture = &quot;dismain3.jpg&quot;;
		//if ($sbgpic != &quot;corey&quot; || $sbgpic != &quot;spike&quot; || $sbgpic != &quot;clown&quot;)  $bgpicture = &quot;dismain1.jpg&quot;;
		echo $sbgpic;


?>


//if ($sbgpic != &quot;corey&quot; || $sbgpic != &quot;spike&quot; || $sbgpic != &quot;clown&quot;) $bgpicture = &quot;dismain1.jpg&quot;;

when i have that part of my code uncommented it the picture wont change at all. when i comment it the pic changes but it doesnt keep the session when i return or refresh. as you see i echo it so i can see if the session does change and it says it does.. i dont know bleh i can make a nice news script but i cant make a picture change haha.

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
The big thing might look ugly, but it is the best and clearest way to show what the variable is.
Code:
$sbgpic = $_SESSION['bgpic']
 $gbgpic = $_GET['bgpic']

 and then change them by like
# these are local variables
$sbgpic = $value

# you need
$_SESSION['sbgpic'] = $sbgpic = $value;

No way around that. Endure the pain.
 
bleh more stressed out.. can you tell me what you see wrong here now :( this thing just doesnt like me at all.. its in an i frame could that be the problem? the session info is passed because when i echo it it shows the right info but it doesnt want to save the back ground for the next time i refresh or visit the site it keeps going back to &quot;corey&quot;. -sigh thanks for your help yes im failing alot but im still learning as i go :D thanks again.

Code:

<?
//starts session
session_start();

//sets variables for easy look
$gbgpic = $_GET['bgpic'];
$sbgpic = $_SESSION['sbgpic'];

//checks if GET and SESSION are the same
if (isset($gbgpic) AND $sbgpic != $gbgpic)
{
//if not it sets the SESSION to the GET
$_SESSION['sbgpic'] = $_GET['bgpic'];
}
//check if SESSION (==) then changed backgroun picture
if ($sbgpic == &quot;corey&quot; || !isset($sbgpic)) $bgpicture = &quot;dismain1.jpg&quot;;
if ($sbgpic == &quot;spike&quot;) $bgpicture = &quot;dismain2.jpg&quot;;
if ($sbgpic == &quot;clown&quot;) $bgpicture = &quot;dismain3.jpg&quot;;

//if SESSION isnt one of the 3 then chose default picture
if ($sbgpic != &quot;corey&quot; || $sbgpic != &quot;spike&quot; || $sbgpic != &quot;clown&quot;) $bgpicture = &quot;dismain1.jpg&quot;;

//debug info
echo $sbgpic;


?>


i tired to comment it so ya know whats going on (im sure you did already) i just dont know what to do.. like i said the links use &quot;switch.php?bgpic=(spike/clown/corey)&quot; bleh and i want to learn to make a shopping cart... fear the sessions!


oh btw.. so with sessions it is fine if i want to name them simple but when i am changing them i need to use the full session thing?

like ok i $candy = $_SESSION['candybar'] in order to change it i should use $_SESSION['candybar'] = $value or do i do that with candy? if $candy is supposed to = the session... man i bet im maken so much out of nothing -cry! im used to application development not web! hehe :D thanks again.. and sorry for being a pain :(



in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Yes, if you don't put the value back into the session, the new value will not be recorded in the session store.

You can manipulate $_SESSION['foo'] all the way through. Nicely self-documenting and explicit coding.

If you want to reduce typing, there is a trick you can use:

$foo = &$_SESSION['foo'];

will make $foo a reference to $_SESSION['foo']. When you manipulate $foo, you're really manipulating the session array element. References are discussed in the PHP online manual here:
Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top