0

i'm sending session values from my first.php and trying to get from my second.php. I have done some reading about this, and works fine on my localhost, but on my server doesn't work at all.

Here is the code from my first.php file:

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title></title>
    </head>
    <body align="center">
        <div id="login">
        <?php

        if (isset($_POST['username']) && isset($_POST['password'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            mysql_connect("localhost", "shopping_katalog", "logik@112233") or die(mysql_error());
            mysql_select_db("shopping_katalog") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
            $result = mysql_query("SELECT password,id FROM x9qg6_users
            where username='" . $username . "'");
            if (!$result) {
                echo 'Could not run query: ' . mysql_error();
                exit;
            }
            $row = mysql_fetch_row($result);

            $userhash = md5($password . $test[1]);

            if ($test[0] === $userhash) {
                $_SESSION['login_user'] = $user_id;
                $_SESSION['username'] = $username;
                $url = "biblioteka.php";
                header("Location: $url");
            }
        } else {
            echo 'Внесете ги вашите податоци во полињата!';
        }
        ?> 
        <form action="" method="POST" accept-charset="UTF-8"> 
            Корисничко име:<br/>
            <input name="username" id="username" type="text"/><br/>
            Лозинка:<br/>
            <input type="password" id="password" name="password"/><br/>
            <input type="submit" value="Логирај се!"/>
        </form>

        </div>
    </body>
</html> 

And here is my second.php file:

<?php
session_start();

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title>Библиотека на</title>
    </head>
    <body>
        <div id="wrapper">
            <?php

            $user_ID = $_SESSION['login_user'];
            $logged_user = $_SESSION['username'];
            ?>
            <h1 align="center" id="b_welcome">Добредојде <?php echo $logged_user; ?> во твојата библиотека! </h1>
            <h4 align="center" id="b_info">Во твојата библиотека ги имаш следниве книги</h4>
            <div id="knigi">
                <?php
                /* OVA E QUERITO
                 * 
                  SELECT *
                  FROM Knigi k, poracki p
                  WHERE k.knigaid = p.kniga
                  AND p.korisnikInt = $user_ID
                 * 
                 */

//-----------------------------------

                if(isset($_SESSION['login_user'])){
                mysql_connect("localhost", "user", "pass) or die(mysql_error());
                mysql_select_db("shopping_katalog") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
                $result = mysql_query("SELECT * FROM Knigi k, poracki p
        WHERE k.knigaid = p.kniga AND p.korisnikInt ='" . $user_ID . "'");
                while ($row = mysql_fetch_array($result)) {

//knigaid,naslov,avtor,link_do_pdf,thumb_link,kategorija,cena,br_strani
                    echo '<div id="item">';
                    echo '<a href="http://' . $row['link_do_pdf'] . '"><h5 align="center" id="b_item_title">' . $row['naslov'] . '</h5></a>';
                    //echo '<a href="http://' . $row['link_do_pdf'] . '">' . $row['avtor'] . '</a>';
                    echo '<a href="http://' . $row['link_do_pdf'] . '"><img src="http://' . $row['thumb_link'] . '" id="b_item_slika" /></a>';
                    echo '</div>';

                }

                if (!$result) {
                    echo 'Проблем со добивање на податоците: ' . mysql_error();
                    exit;
                }
                }else{
                    $url = "/index.php";
                header("Location: $url");
                }
                ?>
            </div><!--kraj na knigite-->
        </div>
    </body>
</html>
Chris
  • 67
  • 1
  • 3
  • 7
  • Are cookies enabled in your server? – Leandro Barreto Dec 06 '12 at 12:05
  • @LeandroBarreto how can I find out? – Chris Dec 06 '12 at 12:09
  • Google: http://stackoverflow.com/questions/6663859/check-if-cookies-are-enabled http://support.qualityunit.com/021373-How-To-Enable-Session-Support-for-PHP – Leandro Barreto Dec 06 '12 at 12:11
  • 1
    "Doesn't work" is **not** helpful. Do you have any logs of $_SESSION ? Can you post your ini_get('session.*') configuration params ? – Justin T. Dec 06 '12 at 12:11
  • I can't see where your values for $test[0] and $test[1] are coming from. Consequently I don't see when "if ($test[0] === $userhash) {" would be evaluated. Were you previously setting a test username and password in an array on your localhost without using the real post values? –  Dec 06 '12 at 12:16
  • And actually, perhaps you mean $row[0] and not $test[0]? –  Dec 06 '12 at 12:20

2 Answers2

0

To check if cookies are enabled or not, use the code...

 <?php
setcookie('test', 1, time()+3600);
if(!isset($_GET['cookies'])){
    header('Location:/info.php?cookies=true');
}
if(count($_COOKIE) > 0){
    echo "Cookies are Enabled!";
} else {
    echo "Disabled";
}
?>
Vishnu R
  • 1,859
  • 3
  • 26
  • 45
0

Here is the concept:

in you first_page.php the user should enter his username and password and if you find them both are correct and match those exist in the database, then you will set a session and store whatever data you want in it, then you will redirect the user to the second_page.php which will use those session stored values to do whatever you want with them. And if the user didn't enter his username and password he will stay in the first_page.php

And here is a simple example from which you can take the main concept and apply it on your case:

in first_page.php

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body align="center">
        <div>
            <?php
                if (!empty($_POST["username"]) && !empty($_POST["password"])) {
                    $_SESSION['username'] = $_POST["username"];
                    header("Location: second_page.php");
                }
            ?> 
            <form action="first_page.php" method="post" accept-charset="UTF-8"> 
                Username: <input type="text" name="username" /><br/>
                Password: <input type="password" name="password"/><br/>
                <input type="submit" value="Login"/>
            </form>
        </div>
    </body>
</html>

And in second_page.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h3>Welcome <?php echo $_SESSION["username"] ?></h3>
    </body>
</html>
Amr
  • 4,809
  • 6
  • 46
  • 60