简体   繁体   中英

i keep coming back to login page in php

newbie here... so yeah, i already tried searching all those page-related about my question, but im still stuck... anyway, my problem is that i always keep getting back at my login page, which is my index.php oh btw, im still using PHP version 4.4.8 here is my code for my problematic main page, main.php

 <?php session_start(); include '../config.php'; if(!isset($_SESSION['admin'])){ header("location:index.php"); } ?> <!DOCTYPE HTML> <html> <head> <title>KSP Setia Finance</title> </head> <body> <h1>test page</h1> </body> </html> 

and here is my login page code, which is index.php

 <?php session_start(); include '../config.php'; ?> <!DOCTYPE html> <html > <head> <title>Login Form</title> </head> <body> <div class="login"> <h1>Login</h1> <form action="login_act.php" method="post"> <input type="text" name="username" placeholder="Username" required="required" /> <input type="password" name="password" placeholder="Password" required="required" /> <button type="submit" name="login" value="Login" class="btn btn-primary btn-block btn-large">Log In</button> </form> </div> <script src="js/index.js"></script> </body> </html> 

since everyone asking, here my login_act.php, already inserted with session_start

 <?php session_start(); include('../config.php'); if(isset($_POST['login'])){ $user = mysql_real_escape_string(htmlentities($_POST['username'])); $pass = mysql_real_escape_string(htmlentities(md5($_POST['password']))); $sql = mysql_query("SELECT * FROM user WHERE username='$user' AND password='$pass'") or die(mysql_error()); if(mysql_num_rows($sql) == 0){ echo 'User not found'; }else{ $row = mysql_fetch_assoc($sql); if($row['level'] == 1){ $_SESSION['admin']=$user; echo '<script language="javascript">alert("u are Login as Admin!"); document.location="index.php";</script>'; }else echo 'sorry, u cant access this one'; } } ?> 

在main.php上打印$ _SESSION的值,并检查是否有任何键作为'username'并检查login.php,您在$ _SESSION数组中存储了什么值

so i recently asking my friends, and here is the results: all i need is just put those $SESSION_START above all, or make another php and link them all. so here my latest result that worked : main.php

 <?php include 'access.php'; ?> <!DOCTYPE HTML> <html> <head> <title>KSP Setia Finance</title> </head> <body> <h1>test page</h1> </body> </html> 
access.php

 <?php session_start(); if(!isset($_SESSION['admin'])){ echo '<script language="javascript">alert("you must Login!"); document.location="../index.php";</script>'; } ?> 

and last, config.php

 <?php session_start(); mysql_connect("localhost","root",""); mysql_select_db("koperasi"); ?> 

i deleted that broken login_act.php, and making all the page i had to be linked directly with the access.php, which make it easier to manage the session. thank you to all that bear with my php problem and stupidity. hope this all gonna help those who still wandering and asking the same question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM