I am building a website where a user logs on from the index.html page. When the user logs on a php $_SESSION['username'] variable is created and then the site redirects the user to a members.php page. If the user returns to the index.html how do I check if the $_SESSION['username'] variable has already been created so the site will redirect the user back to the members.php page.
i don't think you can do that. session is for php only. i really don't see any reason why you wanna use index.html. using index.php is just the same
You cannot use PHP variables on a .html page. You need to save it like index.php then you can check if session is registered: if(session_is_registered("username")){header ("Location: members.php");} PHP:
PHP SESSION variables can only be used in PHP parsed pages. You can either rename your .html page to .php, or modify your Apache config/.htaccess file to interpret all .html files as PHP code. I personally would not recommend the later for security reasons.
Thanks for all the good advice! just for future reference, is there any way to access php variables through javascript?
Since php will process before any javascript as it is server-side, then you can simply echo php variables into javascript like this: alert('<?php echo $_SESSION['My_Session_Var']; ?>'); PHP:
Yes it could be done with ajax/javascript. Your making it more complicated then it really needs to be. Just rename index.html to index.php or use the .htaccess trick of making .html into .php type of files. Then you can just reference the session data yourself using php. Interesting google finds over this very subject. Excellent reading too. php to html (html that works like php ext) http://www.besthostratings.com/articles/php-in-html-files.html ajax check to see if session var has changed. http://stackoverflow.com/questions/3508587/php-ajax-check-if-session-variable-changed great place to start about php sessions http://www.php.net/manual/en/function.session-start.php You could also use php to set cookies and then use ajax to access those cookies var's, but that is way insure.
I don't think this is useful to use such a modifications. Why don't you just change index.html to index.php ? It takes just few seconds.
Hey OP, follow these steps: 1) Rename your index.html file, to index.php 2) Add the following PHP code to the very top of your now, index.php file: <?php if(session_is_registered("username")){header ("Location: members.php");} ?> This code must be at the very top of the page. Not even a space must appear before the code. Try it and GL.
start_session has to be the first thing unless php.ini has it set to auto start then Andre91 code is correct.