Sessions not working

Status
Not open for further replies.

mwhiz

New Member
Messages
8
Reaction score
0
Points
0
Hi,

I transferred my site from another host and everything worked fine so i know my code is not the problem.

It seems as though on this server I cannot write to the $_SESSION variable.

First things first, here is the code for a little test script that i wrote:
PHP:
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Session Test</title>
</head>
<body>
<?php
if ($_POST['session_name'] != "" && isset($_POST['session_name'])) {
$name = $_POST['sessiom_name'];
$value = $_POST['sessiom_value'];
$_SESSION[$name] = $value;
}
?>
<pre>
<?php 
echo "Session:\n========\n";
var_dump($_SESSION);
echo "Post:\n=====\n";
var_dump($_POST);
?>
</pre>
<form id="session_test" name="session_test" method="post" action="session.php">
  <p>Variable Name: 
    <input type="text" name="session_name" id="session_name" />
</p>
  <p>Variable Value: 
    <input type="text" name="session_value" id="session_value" />
</p>
  <p>
    <input type="submit" name="Submit" id="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

You can view this file at http://mwhiz.x10hosting.com/test/session.php.

Try the file yourself, you will see that the session variable is always empty.

Something that I thought of while im writing this is that the old server had php4, and this server has php5, right? Am I using the wrong syntax?

Can anyone help?
 

Jarryd

Community Advocate
Community Support
Messages
5,534
Reaction score
43
Points
48
I am not very sure about PHP, but that might be the problem, this is what i get when i test that page:

Session:
========
array(1) {
[""]=>
NULL
}
Post:
=====
array(3) {
["session_name"]=>
string(4) "test"
["session_value"]=>
string(1) "1"
["Submit"]=>
string(6) "Submit"
}

Not sure what that is :p so yeah..
 

mwhiz

New Member
Messages
8
Reaction score
0
Points
0
I've done some more fiddling around with my script and the problem seems to be trying to assign a variable to a session variable.

For example:

$_SESSION['text_var'] = "1"; WORKS

but

$_SESSION['test_var'] = $_POST['name']; DOES NOT WORK.

Now, I know that the post values are being sent properly because

echo $_POST['name'];

outputs the correct value.

This must be something simple. Please take a look at my test script in the above post.

Please help!
Edit:
I am not very sure about PHP, but that might be the problem, this is what i get when i test that page:

Session:
========
array(1) {
[""]=>
NULL
}
Post:
=====
array(3) {
["session_name"]=>
string(4) "test"
["session_value"]=>
string(1) "1"
["Submit"]=>
string(6) "Submit"
}

Not sure what that is :p so yeah..

Thats just a dump of the values in the $_SESSION variable and the values that were sent through the form (i.e. $_POST). It's just so that people can see what is going on with the variables when they test the script..
Edit:
Request for upgrading your PHP Version to Intermediate at http://x10hosting.com/account

This may solve your problem.


I have done that and my request was approved. Does it take time for the changes to happen once it has been approved?
 
Last edited:

kajasweb

New Member
Messages
1,723
Reaction score
0
Points
0
Try assigning the value and then call session_start() for sending headers to the client.

Edit:
Sorry, You can assign values after calling session_start().

Have you registered the session by calling session_register() ??
 
Last edited:

Jarryd

Community Advocate
Community Support
Messages
5,534
Reaction score
43
Points
48
I think i'll leave this to Kajasweb, i don't understand php much... sorry i couldn't be of more help.
 

mwhiz

New Member
Messages
8
Reaction score
0
Points
0
Try assigning the value and then call session_start() for sending headers to the client.

Edit:
Sorry, You can assign values after calling session_start().

Have you registered the session by calling session_register() ??

That can't be it. According to PHP.net :
- If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled. (such as on x10)
- session_register() is deprecated and $_SESSION should be used instead
 

kajasweb

New Member
Messages
1,723
Reaction score
0
Points
0
TOO Silly.

You have mis-spelled the variable name in your script.

It should be
$name = $_POST['session_name'];
$value = $_POST['session_value'];
 
Last edited:

mwhiz

New Member
Messages
8
Reaction score
0
Points
0
At times like this, all you can do is look at a mirror and laugh.

Cheers mate!
 
Status
Not open for further replies.
Top