andrew_biggart Posted May 28, 2008 Share Posted May 28, 2008 can anyone tell me how to redirect to the page you were currently on, after you login??? Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/ Share on other sites More sharing options...
MadTechie Posted May 28, 2008 Share Posted May 28, 2008 you can use JS, HTML or PHP the PHP one is header("Location: newpage.php"); Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551555 Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 yea im using php but im just wondering do u use variables or ids or what so that after u log in it redirects you to the page u were viewing before you had to log in Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551579 Share on other sites More sharing options...
MadTechie Posted May 28, 2008 Share Posted May 28, 2008 basically you need to grab the details before redirecting to the login page then when the login is check redirect to that page basic example <?php session_start(); //check if the user is logged in if($isLoggedInn) { $_SESSION['page'] = $_SERVER['QUERY_STRING']; header("Location: login.php"); } ?> <?php session_start(); //blar //logged in header("Location: ".$_SESSION['page']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551588 Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 ok i semi understand haha rite im using the following codes my check login code is <?php include ("config.php"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $row = mysql_fetch_assoc($result); $userid = $row['user_id']; $userav = $row['user_avatar']; $name = $row['name']; // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file session_register("myusername"); session_register("mypassword"); $_SESSION['myusername'] = $myusername; $_SESSION['myuserid'] = $userid; $_SESSION['myname'] = $name; $_SESSION['avatar'] = $userav; header("location:my_account.php"); } else { echo "Wrong Username or Password"; } ?> and to check for my session im usuing this code at the top of each page !!! <?php session_start(); if(!session_is_registered(myusername)){ header("location:registration_page.php"); } ?> how do i intergrate this new code into my existing one then ??? Thanks very much by the way !!! Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551597 Share on other sites More sharing options...
MadTechie Posted May 28, 2008 Share Posted May 28, 2008 <?php session_start(); if(!session_is_registered(myusername)) { $_SESSION['page'] = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "/" . $_SERVER['QUERYSTRING'] . "/"; header("location:registration_page.php"); exit(); } ?> and update $_SESSION['myusername'] = $myusername; $_SESSION['myuserid'] = $userid; $_SESSION['myname'] = $name; $_SESSION['avatar'] = $userav; if(!empty($_SESSION['page'])) header("location:my_account.php"); header("location: ".$_SESSION['page']); exit(); Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551634 Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 u are actually a legend i wil test it now Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551667 Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 ok its working to a cetain extent i redirects me to the rite page but its like myaccount.php// any idea why??? this causes the page not to load correctly Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551720 Share on other sites More sharing options...
revraz Posted May 28, 2008 Share Posted May 28, 2008 if(!empty($_SESSION['page'])) header("location:my_account.php"); That's why Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551721 Share on other sites More sharing options...
andrew_biggart Posted May 28, 2008 Author Share Posted May 28, 2008 i still dont understand???? Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551778 Share on other sites More sharing options...
MadTechie Posted May 28, 2008 Share Posted May 28, 2008 if(!empty($_SESSION['page'])) header("location:my_account.php"); That's why i still dont understand???? erm.. me either.. anyways update $_SESSION['page'] = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "/" . $_SERVER['QUERYSTRING'] . "/"; to $_SESSION['page'] = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF']; $_SESSION['page'] .=(!empty($_SERVER['QUERYSTRING'])?"/".$_SERVER['QUERYSTRING']."/":""; should solve it, it was just some quick code Quote Link to comment https://forums.phpfreaks.com/topic/107613-redirecting-after-login/#findComment-551984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.