问题
My website works fine on localhost, but once I deploy it to my hosting service, the session stops working.
<?php
session_start();
session_save_path('log/session.txt');
ini_set('session.gc_probability', 1);
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set("Asia/Jakarta");
require_once 'jadwal/calendar/tc_calendar.php';
include "../metode/data.php";
include "../metode/jalan.php";
include "../metode/perintah.php";
include "../metode/gambar.php";
//session
$queryadmin = mysql_query("select * from root");
$dataadmin = mysql_fetch_row($queryadmin);
$id = $dataadmin[0];
$key1 = $dataadmin[1];
if (isset($_POST['admin'])) {
$_SESSION['id'] = $_POST['x'];
$_SESSION['key1'] = $_POST['xx'];
}
if (isset($_POST['root'])) {
$_SESSION['user'] = $_POST['xxx'];
$_SESSION['key2'] = $_POST['xxxx'];
}
if (isset($_POST['clear'])) {
$_SESSION['id'] = "";
$_SESSION['key1'] = "";
$_SESSION['user'] = "";
$_SESSION['key2'] = "";
}
if (isset($_POST['exit'])) {
$_SESSION['id'] = "";
$_SESSION['key1'] = "";
}
//access
if ($_SESSION['id'] == $id && $_SESSION['key1'] == $key1) {
$user = $dataadmin[3];
$key2 = $dataadmin[2];
if ($_SESSION['user'] == $user && $_SESSION['key2'] == $key2) {
mysql_query("insert into root history values('','".$_POST['day']."','".$_POST['time']."')");
print "<br><br><br>";
include_once "jembatan.php";
} else {
include "root.php";
}
}
else {
include "admin.php";
}
?>
If you need me to show any other files, just ask.
回答1:
Could be the way your hosting set up their PHP. Try printing phpinfo() on the host and check their settings.
To run the phpinfo function, just run a script that looks something like this:
<?php
phpinfo();
?>
The output will include a huge list of settings, but you're looking for this section right her:

Note how my settings says Session Support = enabled. That's what indicates that my host (localhost, in my case) supports sessions.
回答2:
Make sure there is no space or new line before onpenning the <?php
tag, because you code here contains 3 spaces before it.
回答3:
As late as this answer may be coming in, i recently ran into the same problem and after much trial and error. I found out the having the slightest HTML tag above your session could stop session from not registering. So ensure the first thing you have at the top is
<?php
session_start();
?>
来源:https://stackoverflow.com/questions/14541771/php-session-not-working-after-publish-on-web-hosting