this is my first stack overflow question.
I have a home page, where you can login, if you login it sends you to a login php in which you have some code that checks if login is incorrect. If that is the case this code redirects you back to home page:
header("Location: ".SERVER_ADDRESS."home?error=invalid_login");
Now you are back on the home page but the url variable is not set. It does not show in a
var_dump(parse_url($_SERVER['HTTP_REFERER'])); or
var_dump($_GET);
This is my index.php file:
<?php
session_start();
require_once("config.php");
if(!(isset($_GET['page']))) { // redirect to main page
header("Location: ".SERVER_ADDRESS."home");
} else {
$mp = new MainPage($_GET['page']); // redirect to given page
}
?>
If you go try login again with wrong credentials and do the same redirection process it will now show the variable.
Same thing happens if I were to refresh the header after few seconds.
I seem to not understand why the home page would not show the variable straight away, I just want to use the variable for an if statement that would print an extra invalid login message.
I am using wampserver64 for localhosting.
My .htaccess looks like this:
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9a-zA-Z\-\#]+)/?$ index.php?page=$1 [L]
EDIT:
CBroe's answer helped me, my htaccess did not have a QSA flag
Your rule matches on home, and rewrites the request to /index.php?page=home.
You need to add the QSA flag - query string append - to get the new query string you are creating there, merged with any existing one.
Related
I have a domain which I want every attempted url after https://www.example.com/someFolder/ to not give an error but instead give a php page.
For example, I do not have a somefolder/abc.php file but going there will run a process.php file instead and display it there.
I have attempted to modify the 404 page as the process.php but that also modifies the example.com/ error page which I do not want.
It would be great if I do not need to modify/add a file at the root directory
PS. adding a .htaccess to the somefolder folder does work somewhat but then the url shows somefolder/404.php and not somefolder/abc.php
Modify your 404 page as you did putting php script there but check in php if the url that was requested was directory or not. Depending on that make an appropriate action
<?php
$url = $_SERVER[REQUEST_URI];
// check if requested url was ending with slash
$pattern = '#\/$#';
$result = preg_match($pattern, $url);
if ($result === 1) {
//request was to directory suffixed with slash
//do your php code for custom 404
die; //end the page generation
}
?>
request was not to directory ending with slash
put html of regular 404 page here or leave it blank
I have learned that I could turn somefolder into somefolder.php and use the $_SERVER['PATH_INFO'] to make all pages exist.
This is something that can only be done, in the general case, by using the .htaccess file, and redirecting every request like this...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,NC,L]
After that, you can use $_SERVER, $_SESSION, and other global variables in index.php to decide how to handle a 404 error (i.e., you can implement here how to define what a 404 is, etc.).
For instance, $_SERVER['PATH_INFO'] will be able to tell you what URL was requested, and you can use a combination of file_exists() and other calls, to determine if you want to display a full 404, search results, a redirect, or simply to display the results of another script.
This works for example.com/somefolder/, example.com/some/other/folder/, and example.com/a/b/c/d/e/f/g/, etc..
this is my first post so go easy on me.
Basically I am doing some rewrites in my htaccess file to change my made up search friendly URLs into actual URLs, and for the most part they are working. For instance this:
http://www.negativeworld.org/7849/news/nintendo-download-for-may-24-2012
Will turn into this:
http://www.negativeworld.org/article.php?id=7849
Just fine... IF that article exists. If the article doesn't exist, the php code uses this:
header("Location: boarderror.php");
exit;
To bring the user to boarderror.php. This works fine if it the user gets there directly on article.php and the id is bad, but when I am trying to do the htaccess redirect from a search friendly url and the id is bad, the htaccess redirect just hangs for awhile before giving me this message: "The page isn't redirecting properly".
What I want is for it to go to my boarderror.php page when there is a bad id. So basically I want my htaccess page to take a server friendly URL, switch to the true URL, and well... just let go at that point, and the PHP will take it from there. Here is my htaccess line that does the switch:
RewriteRule ^([0-9]+)/(news|review|editorial|podcast)/(.*)$ /article.php?id=$1 [L]
What am I doing wrong? (BTW I realize that if I set up all of my search friendly URLs correctly there should never be a bad id anyway, but I want to be on the safe side...)
Your thoughts aren't wrong. For a wrong ID there is a double redirection which is OK. The problem is how the second redirection happens. Try
header("Location: http://www.negativeworld.org/boarderror.php");
or
header("Location: /boarderror.php");
With your redirection the browser is trying http://www.negativeworld.org/9999/news/boarderror.php (being 9999 the wrong ID) which falls in an endless redirection loop that the browser cuts after 10 tries.
The redirect rule is fine, the issue is in your header function call. When you only provide a file name, the header redirect will send the user to the file in the same folder, much like creating an html link using only the filename.
Let's say i try to load http://www.negativeworld.org/99999/news/nintendo-download-for-may-24-2012 and that id is invalid. In this case it would send send the user to http://www.negativeworld.org/99999/news/boarderror.php which triggers the redirect again and gets stuck in an infinite loop (or would if the browser wasn't smart enough to stop requesting the same URL over and over again).
Per RFC 2616 the location header should provide an absolute URI, so you should do something like this:
header("Location: http://www.negativeworld.org/boarderror.php");
exit;
I setup a 404 page and it works as it should. I am extracting the "error page" from the URL and using that to create a session since I need to pass it as a variable. My 404 has links to different pages and I would like to keep the referring url all the time even if the following pages exist. For example:
www.mysite.com/100
100 does not exist and goes to my 404. The url is shown as step 1.
Inside my 404 I have links to other pages likes www.mysite.com/link.php but I want that to be www.mysite.com/100/link.php
How can I accomplish this?
This is how I am passing the variable I need:
$page2 = $_SERVER['REDIRECT_URL'];
$str2 = substr($page2, 1);
session_start();
$_SESSION['DIST']=$str2;
Any help is appreciated!
You can use $_GET to pass a variable through the URL. Then rewrite
www.mysite.com/link.php?redirect_url=100
To
www.mysite.com/100/link.php
Using .htaccess
RewriteEngine on
RewriteRule ^([0-9]*)/link.php$ link.php?redirect_url=$1
I'm new at Code Igniter. My index page is named abc_welcome where i've below code
public function index()
{
$this->load->library('session');
$login_data = $this->session->userdata('user');
if($login_data) {
redirect('/product/product_details');
}
else {
$this->display();
}
}
So when a already logged in user hit he can redirect to product page. But strangely it is not redirecting.
Where as at login page i've same piece of code and it is redirecting all right.
So when a user come to my www.abc.com page it's not redirecting (if loged in) and session has no data checked but when cliking login button he is and obviously session got the right data. i want him to redirect first time.... more strangely in my demo site (my laptop) Index redirecting is working!!! but not at hostgator.com.
Thanks in advance.
Farness
Thank you all for your support.
i finally made it work by .htaccess redirecting. i just put below code to my .htaccess and it's working as expected.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.abc.com [NC]
RewriteRule ^(.*)$ http://abc.com/$1 [L,R=301]
yes #farness put $config['base_url'] = 'http://www.abc.com/'; in your config.php file
From what you're describing, it seems like your code is fine, if it runs locally on your laptop, so it's likely that the session data isn't being stored/read properly. Aside from the base_url above, you should also verify the following settings in /appication/config.php:
$config['cookie_domain'] should be set to .abc.com (with the leading dot) if your base url is www.abc.com
$config['cookie_path'] just to be safe, set this to "/"
$config['cookie_secure'] set to FALSE (unless your site is accessed only using https, keep this as false
Hopefully that gets you up and running.
Everything was working fine till I added my .htaccess file. What I'm trying to do is route all my users to their profile page. So www.darudude.com/user1 routes to www.darudude.com/userinfo.php?user=user1
My .htaccess file as this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ userinfo.php?user=$1 [L,QSA]
However ever since I added this, it breaks my sessions. On every page I have a initialize a session, and the sessions stores the referrer. This is the piece of code that handles the important part.
if(isset($_SESSION['url'])){
$this->referrer = $_SESSION['url'];
}else{
$this->referrer = "/index.php";
}
//this echo is used to debug why this thing isn't working!!
echo "<script>alert('".$this->referrer."');</script>";
/* Set current url */
$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
and then I'm returned to the original page using this piece of code:
header("Location: ".$session->referrer);
So for example, without the .htaccess file, if I login through one of the pages everything works and I get redirected back to the page I logged in from (i.e. if I logged in from index.php I get redirected back to index.php; if faq.php, I get redirected back to faq.php). With the .htaccess file I keep getting sent to /userinfo.php leading me to thing its something wrong with my rewriterule
This is how its supposed to work:
index.php loads. the $_SESSION['url'] is set to index.php
a login form is enacted whos action redirects to process.php
process.php the $session->referrer is set from $_SESSION['url']
After the login is confirmed the page should redirect using: header("Location: ".$session->referrer);
This is how it worked originally without any problems.
However, after the .htaccess was created it seems to redirect me to userinfo.php. I think it has something to do with my rule.
Any ideas?
I'm not sure if I understand the problem, but the rewrite rule you're using seems to turn the request to /index.php into a request to /userinfo.php?user=/index.php which may not be what you want.
It's because you're relying on $_SERVER['PHP_SELF'], which does not include the query string (the ?user= part of the URI). It worked before because your pages didn't rely on query strings to uniquely identify themselves. You can use $_SERVER['REQUEST_URI'] instead, though look out for circumstances where you don't want the query string to be preserved and now it is being.
Incidentally, the \?* in your RewriteRule regex is doing exactly the same as thing as if it weren't there.
You could try logging in with AJAX, thus never having to refresh the page at all. A simple Google search will throw up plenty of results, see below. Even if you're not using jQuery (which alot of the tutorials seem to expect you to), it's still possible with basic Javascript, in fact that's how I wrote my AJAX log-in script before converting it to use jQuery later.
http://www.google.com/search?q=php+ajax+log-in