unemployment Posted January 25, 2012 Share Posted January 25, 2012 I have written some php that forces login before accessing certain pages on my site. When a user sends an private message to another user, they are notified via email. In the email they can then click the view message link to take them right to the message, but if they aren't logged in they are sent to the login page. I'd like them to be redirected to the message after logging in, but I'm getting a php error. This is currently working for URL's like: domain.com/known-bugs Does NOT work: http://domain.com/messaging?action=read&cid=130&utm_source=new_message&utm_medium=email&utm_campaign=direct_mesage_link I think the query string is a problem in the script. To create this login feature. I am using... $login = array( 'blogadd', 'messaging', 'company-settings', 'company-landing', 'company-create', 'company-join', 'account-settings', 'logout', 'home', 'newsadd', 'partnerRequest', 'known-bugs', 'reminders' ); $page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4); if (in_array($page, $login)) { $urlSource = urlencode(curPageUrl()); header("Location: /login?onlogin=${urlSource}"); die(); } curPageURL is just the full url of the current page. WORKS: When I'm not logged in and I go to the known-bugs page I get redirected to http://domain.com/login?onlogin=http%3A%2F%2Fdomain.com%2Fknown-bugs DOESN'T WORK: When I try and access messaging by clicking... http://domain.com/messaging?action=read&cid=130&utm_source=new_message&utm_medium=email&utm_campaign=direct_mesage_link I get redirected to: http://domain.com/login?onlogin=http%3A%2F%2Fdomain.com%2Fmessaging%3Faction%3Dread%26cid%3D130%26utm_source%3Dnew_message%26utm_medium%3Demail%26utm_campaign%3Ddirect_mesage_link Any thoughts as to what I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/ Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 What is the PHP error? Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311059 Share on other sites More sharing options...
unemployment Posted January 25, 2012 Author Share Posted January 25, 2012 Notice: Undefined index: messaging?action=read&cid=130&utm_source=new_message&utm_medium=email&utm_campaign=direct_mesage_link in /home/www-data/domain.com/login.php on line 177 Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311073 Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 There shouldn't be any problem passing query strings to a header. What is the code for CurPageUrl()? Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311076 Share on other sites More sharing options...
unemployment Posted January 25, 2012 Author Share Posted January 25, 2012 function curPageURL() { if (isset($_SERVER['HTTPS'])) { $url = 'https://'; } else { $url = 'http://'; } if ($_SERVER['SERVER_PORT'] !== '80') { $url .= "{$_SERVER['SERVER_NAME']}:{$_SERVER['SERVER_PORT']}{$_SERVER['REQUEST_URI']}"; } else { $url .= "{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"; } return $url; } Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311089 Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 What is line 177 in login.php? Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311119 Share on other sites More sharing options...
unemployment Posted January 25, 2012 Author Share Posted January 25, 2012 What is line 177 in login.php? Line 177 is where the "You must be logged in" text starts if (isset($_GET['onlogin'])) { $url = urldecode($_GET['onlogin']); if (filter_var($url, FILTER_VALIDATE_URL)) { preg_match('@^(?:http://)?([^/]+)@i', $url, $matches); $url_page_name = end(explode(DIRECTORY_SEPARATOR, $url)); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); if ($matches[0] != 'pitchbig.com') { unset($url); } } else { unset($url); } if (isset($url)) { // print_array($page_names);die(); ?> <p class="errormessage info"> You must be logged in to access our "<?php echo $page_names["{$url_page_name}"]; ?>" page. Please log in below. </p> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/255753-page-redirect-after-login/#findComment-1311134 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.