aebstract Posted February 25, 2009 Share Posted February 25, 2009 I have a header("Location: login.php"); exit; throughout several pages of my website, for various reasons and whatnot. Is there a way to set a session whenever that header is activated? Or will I have to go and edit the code everywhere that is found? Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/ Share on other sites More sharing options...
ram4nd Posted February 25, 2009 Share Posted February 25, 2009 Well if the header is activted then it redirects you to login.php, so you have to put the code there. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-770940 Share on other sites More sharing options...
aebstract Posted February 25, 2009 Author Share Posted February 25, 2009 Yeah I was just wondering if there was something I could do maybe in .htaccess or something similar to run a short script and set a session whenever a specific header is activated. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-770942 Share on other sites More sharing options...
ram4nd Posted February 25, 2009 Share Posted February 25, 2009 What are you planning to do, i am sure that there is easyer way to do that. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-770950 Share on other sites More sharing options...
aebstract Posted February 25, 2009 Author Share Posted February 25, 2009 Certain pages are protected so that if you aren't logged in, it sends you to the login page. After logging in, I'd like to go to the page that was trying to be accessed. You're right that there probably is an easier way. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-770957 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 You can, create an "overrided" header function. Just make sure you put this in a file that is included in every page: function header_($header, $setPage=true) { if ($setPage) $_SESSION['last_page'] = $_SERVER['PHP_SELF']; header($header); return true; } Then just use that, which it should set the session variable and viola. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-771033 Share on other sites More sharing options...
aebstract Posted February 25, 2009 Author Share Posted February 25, 2009 That is checking to see if the header is being ran, period. Right? I'm gonna need something a tad more specific, if the header being ran is header("Location: index.php?page=login"); So I'm gonna need something like function header_(Location: index.php?page=login, $setPage=true) { if ($setPage) $_SESSION['last_page'] = $_SERVER['PHP_SELF']; header(Location: index.php?page=login); return true; } ? Or I'm not sure I understand it Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-771042 Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 Hey There, You can use the function ob_start(); at the beginning of your script, then you can set the session and then run the redirect <?php ob_start(); session_start(); header("location: url"); ?> Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-771047 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 ? Or I'm not sure I understand it Let me explain with an example. function header_($header, $setPage=true) { if ($setPage) $_SESSION['last_page'] = $_SERVER['PHP_SELF']; header($header); return true; } That is added in some file that is included. <?php session_start(); include('functions.php'); // has our header function. if (!$_SESSION['loggedin']) { header_("Location: index.php?page=login"); } ?> Simple as that, now the session contains ['last_page'] in it, the user is redirected where you want them to be and you can then redirect them (with or without using header_) to the page they were trying to access. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-771062 Share on other sites More sharing options...
aebstract Posted March 2, 2009 Author Share Posted March 2, 2009 Whenever I try and echo that session variable out it says: Notice: Undefined index: last_page in /home/virtual/site21/fst/var/www/html/performance/login.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-774904 Share on other sites More sharing options...
aebstract Posted March 2, 2009 Author Share Posted March 2, 2009 *bump Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-774988 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Are you checking if it is set first? Is that header_ being called before you try and call that session variable? If not, then this would not work. You also have to have session_start at the top of the page where you set/try and retrieve this variable at. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-774994 Share on other sites More sharing options...
aebstract Posted March 2, 2009 Author Share Posted March 2, 2009 session_start() is the first line in my index page so it should be included in to every other page as well, as first thing. I set the variable and then try and call it on the next page. Hence it being a variable to see the past page. =/ Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775013 Share on other sites More sharing options...
trq Posted March 2, 2009 Share Posted March 2, 2009 Can we see some relevent code. Were just playing a guessing game at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775018 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Ok so it is set on the index page. how about the page that is being called? Is this a separate page or does it use the index page as a gateway? session_start has to be at the top of any page you want to use sessions on. EDIT: And yes, please do as Thorpe suggested Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775020 Share on other sites More sharing options...
laffin Posted March 2, 2009 Share Posted March 2, 2009 well ya can make that whole process easier, just work on the idea of how it shud run. It sounds like u have some pages ya want to protect, and others are public. well than ya needs a way to let yer script know which pages are what. file: protect.php <?php session_start(); if(!isset($_SESSION['loggedin']) && !defined(PROTECTED)) { $_SESSION['last_page'=$_SERVER['PHP_SELF'] header("Location: login.php"); exit; } file: unprotected.php <?php // Sample Unprotected Page ?> <H1>UnProtected</H1> include("inlcude.php"); [/code] file: protected.php <?php define('PROTECTED',TRUE); include("inlcude.php"); ?> <H1>Protected</H1> good luck Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775033 Share on other sites More sharing options...
aebstract Posted March 2, 2009 Author Share Posted March 2, 2009 all other pages are included in to index. heres an example, products page: <?php if(!isset($_SESSION["id"])) { header("Location: index.php?page=login"); exit; } login.php <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); header("Cache-control: private"); if(isset($_SESSION["id"])) { header("Location: index.php?page=myaccount"); exit; } $error = ""; echo "$_SESSION[last_page]"; index.php <?php session_start(); header("Cache-control: private"); require_once('functions.php'); $content = ''; These are really the only bits that matter, the function is in functions.php Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775042 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); session_start(); // Note session_start added here header("Cache-control: private"); if(isset($_SESSION["id"])) { header("Location: index.php?page=myaccount"); exit; } $error = ""; echo "{$_SESSION['last_page']}"; <?php session_start(); // note session_start here as well. if(!isset($_SESSION["id"])) { header("Location: index.php?page=login"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775045 Share on other sites More sharing options...
trq Posted March 2, 2009 Share Posted March 2, 2009 I don't see anywhere where you actually call this function that sets $_SESSION['last_page']. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775050 Share on other sites More sharing options...
aebstract Posted March 3, 2009 Author Share Posted March 3, 2009 thorpe, the function that was made earlier in this forum is the one used within functions.php which you can see is included in to index.php premiso, I understand it needs to be at the top, but the function that creates the session isn't on the login page, its in functions.php which is simple a php file of functions created. Needs to go top of that page? Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775433 Share on other sites More sharing options...
aebstract Posted March 3, 2009 Author Share Posted March 3, 2009 I placed session_start() at the top of both my login page and my functions page and doesn't give me anything except for: Notice: Undefined index: last_page in /home/virtual/site21/fst/var/www/html/performance/login.php on line 14 Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775458 Share on other sites More sharing options...
aebstract Posted March 3, 2009 Author Share Posted March 3, 2009 Okay just figured out that this won't work: function header_($header, $setPage=true) { if ($setPage) header("Location: index.php?page=thisworks"); return true; } I still go to page=login, whenever I use just that. Like I said before, I don't understand exactly what's going on in this so it's hard for me to really troubleshoot that function. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775464 Share on other sites More sharing options...
trq Posted March 3, 2009 Share Posted March 3, 2009 thorpe, the function that was made earlier in this forum is the one used within functions.php which you can see is included in to index.php That may be so, but Its still not being called anywhere? Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775476 Share on other sites More sharing options...
aebstract Posted March 3, 2009 Author Share Posted March 3, 2009 inside functions.php: function header_($header, $setPage=true) { if ($setPage) header("Location: index.php?page=thisworks"); $_SESSION['last_page'] = $_SERVER['PHP_SELF']; $_SESSION['count_last_page'] = 1; header($header); return true; } inside index.php: <?php session_start(); header("Cache-control: private"); require_once('functions.php'); The functions is pulled in at the top of the index.. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775477 Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 He means where are you calling: "header_" not just "header" You have posted where you defined it. It has to be called. <?php session_start(); // note session_start here as well. if(!isset($_SESSION["id"])) { header_("Location: index.php?page=login"); // note it is now header_ instead of header exit; } Functions have to be called to actually run. You cannot just define them and expect them to run on their own. Quote Link to comment https://forums.phpfreaks.com/topic/146850-solved-set-a-session-when-you-use-header-redirect/#findComment-775512 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.