Сеансы PHP не работают

Я просто не могу перенести свои переменные на следующую страницу. Назначение переменной отлично работает на начальной странице (проверено), но значение становится нулевым при переходе к следующей. Пожалуйста, помогите!

Страница 1:

<?PHP session_start(); ?>
<HTML>
<HEAD>
    <META NAME='robots' CONTENT='noindex,nofollow'>
    <TITLE>Master Chief's Gamestore</TITLE>
    <STYLE type="text/css">
        BODY {
            color: #ffffff;
            background: url('~/halo-reach.jpg');
            background-repeat: no-repeat;
            background-position: top;
            background-color: black;
        }
    </STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
    <tr>
        <td align='center' valign='middle'>
            <table width="50%" cellspacing="0" cellpadding="25" border="0">
                <form method="post">
                    <tr bgcolor="#666633">
                        <th id="header" align="left" colspan="3">
                            <DIV align="center">
                                <font face="Verdana, Arial, Helvetica" color="white" size="3">
                                    &nbsp; Master Chief's Gamestore </font>
                            </DIV>
                        </th>
                    </tr>
                    <tr bgcolor="#A3A385">
                        <td width="100%" valign="middle" colspan="2">
                            <font face="Verdana, Arial, Helvetica" size="2">
                                <div align="center"><br/><br/>
                                    Username:&nbsp
                                    <input type="text" name="username">&nbsp
                                    Password:&nbsp
                                    <input type="password" name="password"><br/>
                                    <br/><input type="submit" name="act" value="Login"><br/>
                                    <input type="submit" name="act" value="Register"><br/>
                                    <input type="submit" name="act" value="Main"><br/> <br/><br/>
                                </div>
                            </font>
                        </td>
                    </tr>
                    <?PHP
                    if ($_POST['act'] == "Login") {
                        mysql_connect("~", "~", "~") or die("unable to connect to server");
                        mysql_select_db("~") or die("unable to select database");
                        $user = $_POST['username'];
                        $pass = $_POST['password'];
                        $query = "select * from users where user like '%" . $user . "%' and pass like '%" . $pass . "%';";
                        $result = mysql_query($query);
                        $rows = mysql_numrows($result);
                        if ($rows == 0 || strlen($user) == 0) {
                            echo "<br />Login Failure";
                        } else {
                            $_SESSION['user'] = mysql_result($result, 0, "user");
                            $_SESSION['id'] = mysql_result($result, 0, "P_Id");
                            header('Location: ~/success.php');
                        }
                    }
                    if ($_POST['act'] == "Register") {
                        header('Location: ~/register.php');
                    }
                    if ($_POST['act'] == "Main") {
                        header('Location: ~/index.php');
                    }
                    ?>
                    <div align="center">
                        <tr bgcolor="#666633">
                            <td align="left">
                                Logged in as:
                                <?PHP
                                $user = $_SESSION['user'];
                                if (!$user) {
                                    $user = 'Guest';
                                }
                                echo $user;
                                ?>
                            </td>
                            <td align="right">
                                Password:&nbsp
                                <input type="password" name="srcpw">
                                <input type="submit" name="dspphp" value="Display PHP">
                            </td>
                        </tr>
                    </div>
                </form>
            </table>
        </td>
    </tr>
</table>
</body>
</html>

Страница 2:

<?PHP session_start(); ?>
<HTML>
<HEAD>
    <META NAME='robots' CONTENT='noindex,nofollow'>
    <TITLE>Master Chief's Gamestore</TITLE>
    <STYLE type="text/css">
        BODY {
            color: #ffffff;
            background: url('~/halo-reach.jpg');
            background-repeat: no-repeat;
            background-position: top;
            background-color: black;
        }
    </STYLE>
</HEAD>
<body vLink='#3366CC' link='#3366CC' alink='#3366CC'>
<br/><br/><br/><br/><br/>
<table width='100%' height='80%'>
    <tr>
        <td align='center' valign='middle'>
            <table width="30%" cellspacing="0" cellpadding="25" border="0">
                <form method="post">
                    <tr bgcolor="#666633">
                        <th id="header" align="left" colspan="3">
                            <DIV align="center">
                                <font face="Verdana, Arial, Helvetica" color="white" size="3">
                                    &nbsp; Master Chief's Gamestore </font>
                            </DIV>
                        </th>
                    </tr>
                    <tr bgcolor="#A3A385">
                        <td width="100%" valign="middle" colspan="2">
                            <font face="Verdana, Arial, Helvetica" size="2">
                                <div align="center">
                                    <br/><br/>Success<br/><br/>
                                    <input type='submit' name='main' value='Main'>
                                    <?PHP
                                    if ($_POST['main'] == "Main") {
                                        header('Location: http://~/index.php');
                                    }
                                    if ($_POST['act'] == "Display Code") {
                                        if ($_POST['pw'] == "pass") {
                                            highlight_file("success.php");
                                        }
                                    }
                                    ?>
                                </div>
                            </font>
                        </td>
                    </tr>
                    <div align="center">
                        <tr bgcolor="#666633">
                            <td align="left">
                                Logged in as:
                                <?PHP
                                $user = $_SESSION['user'];
                                if (!$user)
                                    $user = 'Guest';
                                echo $user;
                                ?>
                            </td>
                            <td align="right">
                                Password:&nbsp
                                <input type="password" name="pw">
                                <input type="submit" name="dspphp" value="Display PHP">
                            </td>
                        </tr>
                    </div>
                </form>
            </table>
        </td>
    </tr>
</table>
</body>
</html>

РЕДАКТИРОВАТЬ: I ' Я пробовал этот пример кода, и он НЕ работает.

Страница 1:

<?php
session_start();
if (isset($_GET['link'])) {
    $_session['myvariable'] = 'Hello World';
    header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
    exit;
}
?>
<a href="<?php print $_SERVER['REQUEST_URI'] . '?link=yes'; ?>">Click Here</a>

Страница 2:

<?php
session_start();
print 'Here is page two, and my session variable: ';
print $_session['myvariable'];
exit;
?>

Но, как я писал ниже, я успешно использовал сеансы на других страницах на том же сервере. Так расстраивает ....

Спасибо за все сообщения, кстати!

5
задан aljo f 3 June 2017 в 15:01
поделиться