I'm trying to figure out how to append a random query string to the end of a URL the first time someone visits my page. For example, say someone navigates to my site at, www.example.com. If everything goes according to plan, they'll hit my index.php file, the URL in the address bar will be www.example.com, and everything will be hunky dory.
However, what I would like is for when that person types www.example.com into their address bar and hits go, they are directed to my index.php file, but the URL is changed to:
www.example.com/?fromdatabase=randomthing
I have the database all setup and I can get random values from it no problem, the problem is changing the URL so that the random values are appended before the page loads.
Add this to the beginning of your index.php
<?php
if(empty($_GET['fromdatabase']))
{
die(header('Location: http://www.example.com/?fromdatabase=randomthing'));
}
?>
Greetings,
Fredyy
You could just redirect the user, if there is no querystring.
For example:
if(!isset($_GET['fromdatabase']))
{
header('location: index.php?fromdatabase=randomthing');
exit;
}
However, it doesnt make much sense. Why do you want to redirect the user to a different url, you could just display the content in your index.
Related
I have an affiliate program and have a serious problem that I can figure out.
My affiliates have links like this...
http://example.net/?p=14&ref=delta88
Once the page loads it changes to...
http://example.net/?p=14
Which totally gets rid of the ref id. I need it to keep the whole URL in the bar in case they hit refresh. Because when you hit refresh it takes the affiliate out of the system and just let's people join without an affiliate.
The way my code works for the pages is this...
That URL goes to an index.php file. In that file it finds all the affiliates information. It then uses an include to show the page. So it's not pointing directly to the page. I need to use the include because I store about 27 pieces of data in strings and I can't put that information in a URL as queries and have it forward to that page.
I added that information because it may be because of the include that's causing it and that will help you better figure out a solution for me.
Use a SESSION, its like a variable that holds for each user, here is a tutorial but works like:
<?php
session_start();
if(isset($_GET["ref"]){
$_SESSION["ref"] = $_GET["ref"];
}
?>
Now, in any PHP that open the user, will have that variable set ( $_SESSION["ref"])
you can keep current url in variable , see below used actual_link to hold the current url data.
if($_GET){
$actual_link = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//if you want to redirect page on same url just call header with $actual_url
header('Location: '. $actual_link);
}
For example, I have a page called profile_page.php. This page is only functional if data is written after the ?u= in the URL, for example, data for Alice's profile page can only be seen when the URL reads http://localhost/profile_page/alice.
Loading http://localhost/profile_page will give me undefined variable errors as most of my variable's are depending on the URL to have a value after the ?u=. For example, the variable $firstname can only be gathered when I get her username in the URL.
In such a case, when http://localhost/profile_page, I would rather have it redirect the user to their own profile_page, but I don't know how I can test the URL and parse it through an if statement.
I understand you can use $u=$_GET['u']; to obtain the current page URL? but I don't think doing this, is the best way to go about it:
$u=$_GET['u'];
if ($u == "http://localhost/profile_page/"){
// redirect to logged in users page code here
}
First, if you are using some parameter for your page to build, the url would looks like httlp://localhost/profile_page.php?firstname=alice&lastname=brown, with $_GET['firstname'] you will get alice in this case. If you want to test if the parameter is set first and redirect to another page if it is not set, you could use
if(!isset($_GET['firstname'])
{
header('Location:redirected_page.php');
}
My current url is http://domain.com/example.php/link=eg But if someone plays with url and puts url as http://domain.com/example.php/abcdefgh97654 - anything, my all functions all links from that page become inactive.
I tried using <?=HTTP_SERVER;?> before all links in my php files but as my website has user registration and sign in, when user signs in and clicks on any menu (link from php script). It throws on index.php page. But if user logs in again it works perfectly. In short user needs to log in twice to work everything perfect.
Basically I want two solutions one I want to redirect www dot
domain dot com/example dot php/abcdefgh97654 - anything (wrong url
page) to errorpage (I already done this in htaccess) but does not
work for above type.
List item
And want to solve two time log in problem.
If anyone has solution for this will be appreciated.
For you to do this, you have to know what values are supposed to be passed via $_GET variable to your page. Then you can create filter for the values and user header(); function.
Here is my suggestion:
<?php
$var=$_GET['val']
//get expected length if you need.
if(strlen($var)>9 or strlen($var)) {
$redirect=true;
}
//if you know what you are expecting
$vals=array('val1', 'val2, 'val3');
if(!in_array($var, $vals)){
$redirect=true;
}
//continue or replace your filters in this manner and asign your value to $redirect
if($redirect==true) {
header("Location:url"); // do this before anything else is printed to the page or else it will say headers already sent.
}
?>
Hey so I have create a login system to a website and I wish to have this login appear when I type in my address. When I have typed in details and logged in, I wish to be redirected to another PHP file, but with the same address.... this way, All I need to do is type in my address if I am allready logged in and I will go to the site which requires login.
I have made a transaction happen identifing if the session is created, if it is, it redirects me to another page, but also to another URL. I tried googleing it, but couldn't find anything exact and straight forward.
Currently:
Login page:
www.example.com
Member page:
www.example.com/members
What I wish for:
Login page:
www.example.com
Member page:
www.example.com
The program structure should look like this.
index.php
if (user is logged in)
display dashoard
else
display login page
Since you are using PHP, make use of session functions. Thus, URL rewriting is no longer necessary.
Update
Assuming if you have file structure in PHP like this:
- index.php
- login.php
+ template
- login.php
- dashboard.php
You can do the following structure in index.php file.
define('IN_FILE', true);
if (isset($_SESSION['user'])) {
require 'template/dashboard.php';
} else {
require 'template/login.php';
}
In template/dashboard.php
if (!defined('IN_FILE')) {
exit;
}
// Then your HTML, PHP and whatnot
And in login.php
if (!isset($_SESSION['user'])) {
require 'template/login.php';
} else {
header('Location: index.php');
}
Change the code according to your needs.
This can be achieved using several approaches.
a) Use session to determine the current page, so if a user click on a link, create a session store the value and on page load read the session data and include the file accordingly.
b) Use URL parameter to determine the page (this is the most common approach). for example in index.php you can add more parameters like index.php?page=somepage and by reading the value using $_GET and including the PHP file accordingly.
There are some more way to achieve what you want to, for instance using javascript/jQuery this is possible.
I know it is really confusing. Let me explain:
I want to open the urls on my site (http://domain.com) to a (http://domain.com/url='the submitted url') and then the submitted url is opened.
Eg: When we open any other site link from Google+ let the example
http://www.youtube.com/watch?v=WRpX7tkwejU
it redirects to
http://plus.url.google.com/url?sa=z&n=1333340186022&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DWRpX7tkwejU&usg=whZv4BO7Gcrco_vivlnhaz27Wpk.
and then the original site is opened. I want some thing similar.
On your site, replace any regular external link like http://example.com with, say, /redirect.php?url=http%3A%2F%2Fexample.com. Make sure to urlencode the original domain name.
In redirect.php, redirect the user:
header('Location: ' . $_GET['url']);