chuflasky Posted March 22, 2011 Share Posted March 22, 2011 Hi, this is not related to the book, i just need some help. i being working in this script for the last past weeks and i still can't figured out. i still get redirected to the index page. I was wondering if i have to modified the login.php in order for the script to be executed. the code below is what a came up with. This is the script where i redirect the user if they are not logged in. <?php session_start(); $page_name = $_SERVER['PHP_SELF']; if(!isset($_SESSION['user_name']) ){ header("Location: login.php" . (isset($page_name)? '?page_name='. $page_name: '' ) ); exit(); }//localhost/index2/user_php/login.php?page_name=/index2/user_php/myevents.php else{ //end of this part require_once "../include/header.php"; //need the data base connection to talk to the data base require_once("../mysqli_conn/db_conn.php"); //select the events fron the data base// $query ="SELECT user_id, title_id, cat_id FROM events WHERE user_id = '$username' "; $result = mysqli_query($dbc, $query) or die("Error: " . mysqli_error($dbc) ) ; $rows = mysqli_num_rows($result); if($rows == 0){ echo "<p>You current have no events posted so far.</p>"; }else{ echo "<table cellspacing='3' cellpadding='3' width='50%'>"; while($rows = mysqli_fetch_array($result,MYSQLI_ASSOC) ){ echo ' <tr> <td>Events Name:</td> <td>' . $rows["name"]. '</td> </tr> '; } echo" </table>"; } }//end of else stament include("../include/footer.html"); ?> Now, i get redirected to login_page_inc.php. the below script is what i got. <?php include("../include/header.php");?> <?php /* *login_page_inc.php */ //this pega prints any errors associated with logging in //and it create the entire login page, including the form. ///include the header //print any errors message if they exist ///get the page url //this part goes in the header if(isset($_GET['page_name']) ){ $page_name = $_GET['page_name']; }elseif(isset($_POST['page_name']) ){ $page_name= $_POST['page_name']; } //end of this part //check to see if already logged in if(isset($_SESSION['user_name']) ){ //redirect if page has a value. if(isset($page_name) && strlen($page_name) >0){ $url = $page_name; header("Location: $url"); exit(); }else{ //redirect to home page $url = "../index.php"; header("location: $url"); exit(); } } if(!empty($errors) ){ echo "<div id='signIn_error_handler'>"; echo "<h2 class='error_2'>Error!</h2> <p class='error_2'>The following error(s) occured:<br />"; foreach($errors as $msg){ echo " $msg<br />\n"; } echo "</p><p class='try_again'>Please Try again.</p><p><br /></p>"; echo"</div>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sing In</title> <link href="../css/php_style.css" rel="stylesheet" media="all" type="text/css" /> </head> <body> <div id="wrapper"> <h2 class="signIn_form">Sign In Form</h2> <div id="user_signIn"> <div id="image"> <img src="../images/user-security.png" /> </div><!--end of image --> <table cellpadding="3" cellspacing="3" width="50%"> <tr> <form action="login.php" method="post" > <tr> <td><p>User Name:</p></td> <td><input type="text" name="username" maxlength="30" value="<?php if(isset($_POST['username']) )echo $_POST['username'] ?>" /> </td> </tr> <tr> <td><p>Password:</p></td> <td><input type="password" name="password" maxlength="15" /> </td> </tr> <tr> <td> <input type="hidden" name="page_name" value="<?php echo $_POST['page_name']; ?>" /> <input type="hidden" name="page_name" value="<?php echo $_GET['page_name']; ?> " /> <input type="hidden" name="submitted" value="TRUE" /> <input type="submit" name="submit" value="Sign In" /> </td> </tr> </form> </tr> </table> </div><!--end of user_signIn --> </div><!--end of wrapper --> </body> </html> <?php include("../include/footer.html");?> And when i view page source, the hidden field with the $_GET variable has the value from which it came from. But still get redirected to the index.php. I was wondering if the login_function_inc.php and login.php has to be modified. and the reason that i think so, is because the form is being process by the login.php. and in the login.php below. <?php /* *login.php *this page processes the login form submissin. *upon succeful login, the user is redirected. *two include file are necessary. *set nothin to the web browser prion to the set cookie() line! */ //chech if the form has been submitted if(isset($_POST['submitted']) ){ //for processing the login require_once("login_function_inc.php"); //need the data base connection: require_once("../mysqli_conn/db_conn.php"); list($check, $data) = check_login($dbc,$_POST["username"],$_POST["password"]); if($check){// if ok session_start(); $_SESSION['member_id'] = $data["member_id"]; $_SESSION['user_name'] = $data["user_name"]; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); /*setcookie('member_id',$data['member_id'], time()+3600*24*7*365, '/','', 0, 0 ); setcookie('user_name', $data['user_name'], time()+3600*24*7*365, '/','',0 ,0); */ //redirect $url = absolute_url("../index.php"); header("Location: $url"); exit();//quit the scrip } else{ //unsuccessful! //assign $data to $errors for error reporting //in the login_page_inc.php. $errors = $data; } mysqli_close($dbc);// close the data base connection }//end of if submitted //creat the page: include("login_page_inc.php"); ?> As i point in red from about that the redirection already set, if i change that to $url = absolute_url($page_name); and remove the code dirname($_SERVER['PHP-SELF']) to just $url ='http://' . $_SERVER['HTTP-HOST']; in the login_function_inc.php script, it redirect me back to the requested page, but when i try to login normally, it take me to XAMPP main page. Link to comment Share on other sites More sharing options...
chuflasky Posted March 31, 2011 Author Share Posted March 31, 2011 Problem solve. posting solution for the benefit of others. 1: in the page that i want to redirect back i create a variable that store the current page URL. then i add that variable to the header that take the user to the login_page if they are not logged in. $page_name = $_SERVER['PHP-SELF']; if(!isset($_SESSION['user_name'])){ header("Location: login_page_inc.php" . (isset($page_name)? '?page_name=' . $page_name : '') ); } After being taken to the login_page_inc.php, retrieve the URL that the page came from using a hidden field in the login_page_inc.php. <input type = "hidden" name="page_name" value="<?php echo $_GET['page_name']; ?>" /> Now and this is the most important. in the login.php not the login_page.inc.php on page 338 of the book PHP 6 AND MYSQL5 'Larry ullman', line 26 to 29 i made the following changes. if(isset($page_name)){ $url = page_name; header("Location: $page_name"); $url = absolute_url("../index.php"); header("Location: $url" , false); exit(); } I referred to the book for the benefit of the google search engine. 1 Link to comment Share on other sites More sharing options...
Larry Posted April 1, 2011 Share Posted April 1, 2011 Sorry, Jorge, that I didn't get you an answer. There was a ton of code to review, so I put it on my watch list to get back to it and...apparently I never did. Sorry about that. Kudos for figuring it out and thanks for posting the solution. Link to comment Share on other sites More sharing options...
chuflasky Posted April 6, 2011 Author Share Posted April 6, 2011 No problem Larry, it happens. Link to comment Share on other sites More sharing options...
Recommended Posts