I was learning PHP and trying to redirect pages based on if the cookie is set or not so below is the code I used to set the cookie on the first page
<?php
setcookie("test","logged in",time()+60,'/');
?>
Now on the testing page I delete the cookie but it doesn't get deleted below is the code
<?php
setcookie("test", 0, time()-(60*60*24*7));
if(isset($_COOKIE['test']))
{
echo "u had logged in";
}
else
header("Location: index.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
What exactly is the problem ?
Cookies are only set when a page is transmitted to the browser and are only read when they're sent to the server as a part of a HTTP request.
As such:
If you delete a cookie, it won't disappear until the next page load.
If you set a cookie, the value can't be read until the next page load.
A common way of dealing with this is to set/delete a cookie and then perform a re-direct.
Changes made to cookies are only visible to the server on refresh. If you reload the testing page, you shouldn't see the "logged in" text.
Related
I'm using Codeigniter and I did the internationalization using :
CI-internationalization
I would like to add /en by default when the user access my site :
www.mysite.com -> www.mysite.com/en
What is the best way to achieve that ?
Thank you.
HTML redirects
The simplest way to redirect to another URL is with the Meta Refresh tag. You can place this meta tag inside the <head> at the top of any HTML page like this:
<meta http-equiv="refresh" content="0; URL='http://www.new-site.com/en'" />
The content attribute is the delay before the browser redirects to the new page, above example it is set to 0 seconds.
JavaScript redirects
Redirecting to another URL with JavaScript is pretty easy, we simply have to change the location property on the window object:
window.location = "http://www.new-site.com/en";
OR
window.location.href = "http://www.new-site.com/en";
OR
window.location.assign("http://www.new-site.com/en");
OR
window.location.replace("http://www.new-site.com/en");
PHP redirects
<?php
header('Location: http://www.new-site.com/en', true, 301);
exit();
?>
This has to be set before any markup or content of any other sort, however there is one small hitch. By default the function sends a 302 redirect response which tells everyone that the content has only been moved temporarily. Considering our specific use case we'll need to permanently move the files over to our new website, so we'll have to make a 301 redirect instead.
The optional true parameter above will replace a previously set header and the 301 at the end is what changes the response code to the right one
Source: https://css-tricks.com/redirect-web-page/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Testing page
</title>
<meta http-equiv="refresh" content="0; URL='new/test.html'" />
</head>
<body>
Old page
</body>
</html>
second/new page
<html>
<head>
<title>new site
</title>
</head>
<body>
this is new page.
</body>
</html>
I am trying to get my website to redirect to the home page after it submits the contact form, and goes to the thank you page. I have it working well, other than redirecting AFTER thank you page, it works for if I delete thank you page. I know I need an of statement after the line and then the header(www.google.com) type thing, but not sure what to put for the if statement.
I uploaded my contact form files into the dropbox link below, if someone could get me on the right track that would be awesome. Thanks in advance.
https://www.dropbox.com/sh/2zrci8b04989u3d/gEc3u6rPK4
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Thank you!</title>
<link rel="STYLESHEET" type="text/css" href="contact.css">
</head>
<body>
<script>
window.onload=function() {
setTimeout(function() {
location.replace("index.php");
},3000); // wait 3 seconds
}
</script>
<h2>Thank you...</h2>
click here if you are not redirected in a few seconds
</body>
</html>
Without looking at the pages since I am on my phone, put this in thank you page
<script>
window.onload=function() {
setTimeout(function() {
location.replace("somepage.php");
},3000); // wait 3 seconds
}
</script>
<h2>Thank you...</h2>
click here if you are not redirected in a few seconds
Try something like this, in case the user does have javascript disabled:
<?php
header( "Refresh: 5 url=http://yourdomain.com/" );
?>
<!DOCTYPE html>
<html>
<!-- HTML CODE GOES HERE -->
</html>
The number 5 in the header() function is the amount of seconds before the page redirects the user the url.
The header function Must come before any html and/or php outputs
You can do this in 3 ways.
1 - Html redirect :
<!doctype html>
<head>
<META http-equiv="refresh" content="0;URL=http://www.example.com">
</head><body></body></html>
2 - PHP Header redirect :
<?php
header('Location: http://www.example.com');
?>
<!doctype html><html><head></head><body></body></html>
3 - Javascript Redirect :
<!doctype html><html><head>
<script>
window.location.assign("http://www.example.com");
</script>
</head><body></body></html>
Ok so I'm not really sure how to explain the problem but here goes:
i have a login that gets checked if the input corresponds with the data in my database.
If it does, i create a $_SESSION["login"]. And the user is now logged in.
this session gets created without a problem. So here's my code I use to check if the session is present:
session_start();
if(!isset($_SESSION["login"])){
$_SESSION["denied"]= "You need to be logged in to access this page";
header("location:index.php");
}
now when I go to for instance to my page 'cart' (link located on my index.php page) i put the above code at the beginning of the page, since you need to be logged in to view this page. However i can see the browser load that page, but nothing happens. it is as if for a very tiny second I'm at my page 'cart' but that it...
Can anybody help me out with this one. PHP is mostly a mystery to me.
thanks!
OK, here's an update on the code. This part is what i put in my 'cart.php
<?php
session_start();
include("inc/database.php");
if(!isset($_SESSION["login"])){
$_SESSION["denied"]= "You need to be logged in to access this page";
header("location: index.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<title>Cart</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
</head>
<body>
testing if the page shows correctly
</body>
</html>
And this is my code for my login check
<?php
session_start();
include("inc/database.php");
include("inc/clean.php");
if(isset($_POST['login'])){
$username2=clean($_POST['username2']);
$password2=clean($_POST['password2']);
$query=("SELECT *
FROM signup
WHERE username='$username2'
AND password='$password2';");
$result=mysql_query($query);
$count=mysql_num_rows($result);
$query2=("SELECT id
FROM signup
WHERE username='$username2';");
$result2=mysql_query($query2);
while(list($id)=mysql_fetch_row($result2)){
echo($id);
}
if($count == 1){
$_SESSION["username"] = $username2;
header('location:index.php');
}else{
$_SESSION["error"] = "error";
header('location:index.php');
}
}
?>
I think the issue is with the header instruction, this is the correct one:
header("location: index.php");
Probably your code reaches this instruction but the redirection fails.
And of course check the php log and see if erros are reported...
I have a test.php page
in this page , there is a url . i need to execute that url (here database updation is doing).This is my code
<?php
$username ='testUsername';
if($_GET['age']!=''){
header('location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username); //need to updatethe age of this username
$show ='hello';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $show ?>
</body>
</html>
i know this code is not going to work properly.How can i write in a good way.I donot want to redirect the page.I just need to execute that link
Just call file_get_contents on the url in question instead of setting the location header to that value.
Since the update.php file is hosted on an external website, the only thing you can do is get the contents of the output of the file. (Use file_get_contents to get that output.) That is, it will call the file (with your parameters) and you can fetch the HTML result of it—nothing more. It would be a major security problem if server files could be executed on external websites.
If you need to include the code in update.php without redirecting, you can do so with the include or require functions.
require() is identical to include()
except upon failure it will ... halt
the script whereas include() only
emits a warning (E_WARNING) which
allows the script to continue.
Use the IMG tag. <img widht=0 height=0 src="<?='location:www.test.com/update.php?age='.$_GET['age'].'&username='.$username ?>" />
I have actually discovered my problem but I am really want to know why this is an issue. I had two pages form1.php I started a session on that page and hit submit. Then I had a link to session2.php which started that session and was able to pull the information from form1.php. I am just learning about sessions and this was a very simple exercise to learn what a session can do.
Here lies the issue, I had a stylesheet link in my head and it had a blank href well it was href="#" and when that was there the session2.php would not start the session from form1.php and grab the info from the form. Without that href="#" in the style tag it worked fine, and it also worked fine if it was a fake styletag href="something.css" but href="" doesn't work either.
Why is this? I only have those in because its a template I made for workflow, maybe I cant include the css link in my template anymore to prevent future issues.
You can see this site working here, if I haven't explained myself.
form1.php
<?php
session_start();
$_SESSION['name'] = $username;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<!--//CSS STYLESHEETS//-->
<link rel="stylesheet" href="#" type="text/css">
</head>
<body>
Go to session 2
<!--form stuff is in here-->
</body
session2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
session_start();
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Your second page needs to look like this:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Note that session_start() must appear before any content is printed to the screen.
Per the note on the session_start PHP manual page:
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
To work like you want it, you need to have started the session first. Sounds simple, because it is. When you say session_start, php then looks for an accepted session cookie first to process content.
From http://php.net/manual/en/function.session-start.php
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
Are you trying to output stuff to the page before you send the headers? What happens if you put the stylesheet after you call session_start()?