PDA

View Full Version : subdomains, sessions, and session variables


John
02-18-2003, 12:23 PM
This is some code I wrote to extract the subdomain prefix from a subdomained site. I then assign a session variable with that value to use in the main site.

The subdomain I am using for experiements is http://az.citysave.net



<?php



session_start();



$stateAddress = $_SERVER["PATH_TRANSLATED"];

$stateAddressArray = explode(".", $stateAddress);

$stateAbreviation = explode("/", $stateAddressArray[0]);

$state = $stateAbreviation[count($stateAbreviation) - 1];

$_SESSION['state'] = $state;



header("Location: http://www.citysave.net/");

exit;

?>



Here is the index.php code for the site www.citysave.net



<?php



session_start();



// echo "State = ".$_SESSION['state'];

// This only prints out "State = " with no value for the session variable.



if(isset($_SESSION['state']))

{

$stateAbreviation = $_SESSION['state'];

$bodyContent = '

<form name="form1" method="post" action="submission.html">

<div align="center">Enter your city code here:

<input type="text" name="textfield">

<input type="submit" name="Submit" value="Submit">

</div>

</form>

';

}

else

{

$stateAbreviation = '';

$bodyContent = '<form name="form1" method="post" action="azindex.html">

<div align="center">

<p>Select your state:

<select name="select">

<option>Arizona</option>

</select>

</p>

<p>

<input type="submit" name="Submit" value="Go">

</p>

</div>

</form>

';

}



include("phf.inc.php");

?>



I am using files to handle the sessions, but the session variable "state" does not come through.

To see the session settings for the server, go to www.citysave.net/phpinfo.php

Any suggestion would be greatly appreciated!

Thanks!

PS.

These scripts work just fine on my local machine, but don't on the remote host.

There are two differences between the two that I think may be significant, but I'm not sure how.

They are:

local: PHP v 4.2.3 and session.use_trans_sid = 0

remote: PHP v 4.2.2 and session.use_trans_sid = 1

Anyone?

dbmasters
02-18-2003, 01:33 PM
I will have think about this for a bit, but off the top of my head, how about try saving the State choice in a cookie, rather than a session.

By default sessions use cookies to store their session info ID, but by bypassing sessions and just writting az to a cookie you could at least determine if the problem is with how sessions are being handled.

Just a thought...

John
02-19-2003, 03:57 AM
Alright... I changed to cookies and it worked... But, I don't want cookies. I want the value to expire on the closing of the browser window, not persist beyond it.

I'm clueless as I have used custom database stored sessions, and this is the first site I've tried using the native SESSION handler built into PHP.

John
02-19-2003, 04:01 AM
I was just thinking, there has GOT to be someone hosted on these servers who is using the "session_start();" function and either the "session_register()" or "$_SESSION[]" assignments that has made it work.

Anyone want to fess up? :D

dbmasters
02-19-2003, 07:27 AM
Well, you can just leave the "expires" value of your cookie blank and it will expire at the end of the browser session. PHP documentation does not say that is how it works, but that is how it has always worked for me.

Personally, I have never had much luck with sessions in PHP due to problems with load balancing and other variables that interfere with it so I have always used cookie.

Besides, sessions use cookies anyway unless you explicitly pass the SID in links.

John
02-19-2003, 11:24 AM
I tried leaving the cookie's expire field blank, but get errors... here is the cookie as it is now:

setcookie('state', $state, time() + 10, ".citysave.net");

I tried this:

setcookie('state', $state);

but that did not work. Could that NOT have worked because of the redirection immediately after it?

Thanks!

John
02-19-2003, 10:20 PM
Did I lose you?

dbmasters
02-20-2003, 07:45 AM
no, it should still have worked, but for the sake of trouble shooting why not try removing the redirect and see what happens.