Login Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

Login Problem

Post by aquilina »

i having issue with login. the main page is index and after i login as student, it still stay at index but it display blank body and footer part.. below is my student db connect code.. After login as student it should bring me to the student_home.php..

Code: Select all

<?php
            if (isset($_POST['login'])) {

                function clean($str) {
                    $str = @trim($str);
                    if (get_magic_quotes_gpc()) {
                        $str = stripslashes($str);
                    }
                    return mysql_real_escape_string($str);
                }

                $username = clean($_POST['username']);
                $password = clean($_POST['password']);

                $query = mysql_query("select * from student where username='$username' and password='$password'") or die(mysql_error());
                $count = mysql_num_rows($query);
                $row = mysql_fetch_array($query);


                if ($count > 0) {
                    session_start();
                    session_regenerate_id();
                    $_SESSION['id'] = $row['student_id'];
                    header('location:student_home.php');
                    session_write_close();
                    exit();
                } else {
                    header('error_login.php');
                  
                    ?>

                    <?php
                }
            }
            ?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Login Problem

Post by Celauran »

Is this at the very top of your page? What errors are you seeing? Both session_start and header redirects need to be called before any output has been sent to the browser.
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

Re: Login Problem

Post by aquilina »

Celauran wrote:Is this at the very top of your page? What errors are you seeing? Both session_start and header redirects need to be called before any output has been sent to the browser.
Not every page.. only student php.. Theres no error occur.. yeah i called the session_start and header at the top.. here my code..

Code: Select all

<?php
include('header.php');
//Start session
session_start();
//Unset the variables stored in session
unset($_SESSION['id']);
?>
<body>

    <?php include('navhead.php'); ?>

    <div class="container">
        <div class="row-fluid">
            <div class="span10">
                <ul class="breadcrumb">
                    <li class="active">Login<span class="divider">/</span></li>
                    <li><a href="login_student.php"><i class="icon-group icon-large"></i>&nbsp;Teacher</a><span class="divider">/</span></li>
                    <li  class="active"><i class="icon-group icon-large"></i>&nbsp;Student</li>

                    <div class="pull-right">       
                        <li>   
                            <i class="icon-calendar icon-large"></i>
                            <?php
                            $Today = date('y:m:d');
                            $new = date('l, F d, Y', strtotime($Today));
                            echo $new;
                            ?>
                        </li>
                    </div>  
                </ul>

                <div class="alert alert-info">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>Login Student!</strong>&nbsp;Please Enter the Details Below.
                </div>

                <form class="form-horizontal" method="post">
                    <div class="control-group">
                        <label class="control-label" for="inputEmail">Username</label>
                        <div class="controls">
                            <input type="text" name="username" id="inputEmail" placeholder="Username">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="inputPassword">Password</label>
                        <div class="controls">
                            <input type="password" name="password" id="inputPassword" placeholder="Password">
                        </div>
                    </div>


                    <div class="control-group">
                        <div class="controls">
                            <button type="submit" name="login" class="btn btn-info"><i class="icon-signin"></i>&nbsp;Sign in</button>
                        </div>


                    </div>

                    <?php
                    if (isset($_POST['login'])) {

                        function clean($str) {
                            $str = @trim($str);
                            if (get_magic_quotes_gpc()) {
                                $str = stripslashes($str);
                            }
                            return mysql_real_escape_string($str);
                        }

                        $username = clean($_POST['username']);
                        $password = clean($_POST['password']);

                        $query = mysql_query("select * from user where username='$username' and password='$password'") or die(mysql_error());
                        $count = mysql_num_rows($query);
                        $row = mysql_fetch_array($query);


                        if ($count > 0) {
                            session_start();
                            session_regenerate_id();
                            $_SESSION['id'] = $row['user_id'];
                            header('location:student_home.php');
                            session_write_close();
                            exit();
                        } else {
                            session_write_close();
                            ?>
                            <div class="pull-right">   
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                <div class="alert alert-danger"><i class="icon-remove-sign"></i>&nbsp;Access Denied</div>
                            </div>
                            <?php
                            exit();
                        }
                    }
                    ?>

                </form>




            </div>
            <div class="span2">

                <div class="hero-unit-1">
                    <ul class="nav  nav-pills nav-stacked">
                        <li class="nav-header">Links</li>
                        <li><a href="index.php"><i class="icon-home icon-large"></i>&nbsp;Home</a></li>
                        <li><a href="#"><i class="icon-file-alt icon-large"></i>&nbsp;New And Events</a></li>
                        <li><a href="#"><i class="icon-sitemap icon-large"></i>&nbsp;Site Map</a></li>
                        <li><a href="#"><i class="icon-envelope-alt icon-large"></i>&nbsp;Contact Us</a></li>
                        <li class="nav-header">About US</li>
                        <li><a href="#"><i class="icon-book icon-large"></i>&nbsp;Mission</a></li>
                        <li><a href="#"><i class="icon-book icon-large"></i>&nbsp;Vision</a></li>
                        <li><a href="#"><i class="icon-list-alt icon-large"></i>&nbsp;History</a></li>

                    </ul>
                </div>

            </div>
        </div>
        <?php include('footer.php'); ?>
    </div>
</div>
</div>






</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Login Problem

Post by Celauran »

aquilina wrote:Theres no error occur..
Yes of course there is. Make sure error reporting is turned on.
here my code..
And there's your problem as I already described above; you're trying to use a header redirect after output has been sent to the browser i.e. after headers have already been sent.
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

Re: Login Problem

Post by aquilina »

Celauran wrote:
aquilina wrote:Theres no error occur..
Yes of course there is. Make sure error reporting is turned on.
here my code..
And there's your problem as I already described above; you're trying to use a header redirect after output has been sent to the browser i.e. after headers have already been sent.
what did u mean by my error reporting is turned off? it was on since i working on some error previously.. let me try it out about the header..
Post Reply