Jump to content

[SOLVED] Sessions won't work


jbooth952

Recommended Posts

I mostly copied the code below from samples in tutorials, but it won't work for me.

 

The "if (isset)" always takes the "else" path, and the page linked to, never finds the counter.

 

Do I have to set other parms? Do I have to change the php.ini?

 

Help!

 

<?php  session_start();

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

  {  $_SESSION['counter'] = $_SESSION['counter'] + 1;

print "at the if " . $_SESSION['counter'];

  }

  else

  {  $_SESSION['counter'] = 1;

print "at the else " .  $_SESSION['counter'];

  }

 

  $msg = "You have visited this page ".  $_SESSION['counter'];

  $msg .= " times in this session.";

?>

<html>

<head>

<meta http-equiv="Content-Script-Type" content="text/javascript" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="keywords" content="HTML, XHTML, JavaScript" />

<title>Setting up a PHP session</title>

</head>

<body>

<?php  echo ( $msg ); ?>

<p>

To continue click following link <br />

<a  href="newpage.html">different page</a>  !--

</p>

</body>

</html>

Link to comment
Share on other sites

try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir

 

In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I  correctly point there.

 

What commands change/set the system to point to that directory? And, do they go before or after session_start() ?

 

When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions.

 

Thank you.

Link to comment
Share on other sites

You don't change anything in your code, you change the php.ini file to that path.

 

I'm on a hosting service. Does each account have its own php.ini? Can I change it? Then, I guess if I can, I need to find out where it is, and how to change it on the host's server.

 

Thanks,

 

Jim

Link to comment
Share on other sites

well it depends on the how php is setup on the server.

 

if you have .htaccess file in your root directory then use

php_value session.save_path '/tmp'

 

or if your hosting has provided you with php.ini file in your root dir then you might use

session.save_path = "/tmp"

 

or otherwise try to add this code of line before session_start() in your script

ini_set('session.save_path','/tmp');

 

where /tmp is the directory path that you will have to change to whatever directory you have access to

 

try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir

 

In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I  correctly point there.

 

What commands change/set the system to point to that directory? And, do they go before or after session_start() ?

 

When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions.

 

Thank you.

Link to comment
Share on other sites

I don't know if you want to bother with this, but if you're curious read on;

 

If you go to this URL http://firstpickclub.com/session_test.php

 

it works correctly. Each time you refresh, the counter increases. This page contains ONLY PHP code as follows with NO html;

 

<?php

session_start();

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

  $_SESSION['views']=$_SESSION['views']+1;

else 

$_SESSION['views']=1;

echo "Views=". $_SESSION['views'];

?>

 

Now, I copied that code into another file that you can browse;

 

http://firstpickclub.com/v1sessiontest2.php

 

It has the following code (the above code plus html), and it does not increase the count on refresh; UNLESS, while still in the browser, I go to the first URL that has no html, and refresh it once, and then go back to this page and click refresh twice. This is very strange.

 

<?php

session_save_path("the path they provided removed for security") it acts the same with or without this line, BTW

session_start();

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

  { $_SESSION['views']=$_SESSION['views']+1; }

else 

  { $_SESSION['views']=1; }

 

echo "Views=". $_SESSION['views'];

 

?>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

   "http://www.w3.org/TR/html4/loose.dtd">

 

<html>

<head>

<meta http-equiv="Content-Script-Type" content="text/javascript" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="keywords" content="HTML, XHTML, JavaScript" />

<title>Setting up a PHP session</title>

</head>

 

<body>

 

<?php $msg = $_SESSION['views']; ?>

 

<p>

<?php echo "In body count = " . $msg; ?></p>

 

<p>

To continue click following link <br />

<a  href="v1newpage.php">different page</a>

</p>

 

</body>

</html>

 

well it depends on the how php is setup on the server.

 

if you have .htaccess file in your root directory then use

php_value session.save_path '/tmp'

 

or if your hosting has provided you with php.ini file in your root dir then you might use

session.save_path = "/tmp"

 

or otherwise try to add this code of line before session_start() in your script

ini_set('session.save_path','/tmp');

 

where /tmp is the directory path that you will have to change to whatever directory you have access to

 

try changing the session path in php.ini to some directory on your system where you want to store them, may be in some tmp dir

 

In searching my web host's knowledge base, I found that I need to change the path where the session info is stored. They gave me the real path name, but they didn't tell me how to code the script so that I  correctly point there.

 

What commands change/set the system to point to that directory? And, do they go before or after session_start() ?

 

When you're a customer on a webserver host, can you change the php.ini itself? I've been a programmer for many years, but I'm new to client-server and PHP, so sorry for the dumb questions.

 

Thank you.

Link to comment
Share on other sites

Unbelievable!

 

The source code was saved from Notepad as UTF-8 and I had charset=UTF-8 in the HTML.

 

I saved the source as ANSI and changed the charset to windows-1252 and now it works.

 

Why is this the case? Is it because I use a Windows server?

Link to comment
Share on other sites

Means you were getting a Error that the session couldn't start because of output, and you have error display turned off.

 

Unbelievable!

 

The source code was saved from Notepad as UTF-8 and I had charset=UTF-8 in the HTML.

 

I saved the source as ANSI and changed the charset to windows-1252 and now it works.

 

Why is this the case? Is it because I use a Windows server?

Link to comment
Share on other sites

You could have found that problem three days a go by following what this post stated -

Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

I'm new to PHP and didn't even know about that until 2 days ago, and I didn't think that had anything to do with character sets. I still don't understand why the character set should affect commands.

Link to comment
Share on other sites

You could have found that problem three days a go by following what this post stated -

Add the following two lines immediately after your <?php tag (move the session_start(); on that line down) -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

I'm new to PHP and didn't even know about that until 2 days ago, and I didn't think that had anything to do with character sets. I still don't understand why the character set should affect commands.

 

Now I'm really confused. I went back and copied the new, working pages that are in ANSI format, added the error display code above and saved them as UTF-8  to see what errors would show up, and now the utf-8 format pages work just fine, and no errors show.

 

What the he!!?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.