This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example
Related
This is the index page, from which the user is redirected to the payment page where the payment is made. Once the payment is made, I want that the user is then redirected to the success page, which eventually redirects to the details page of the "specific" item for which the payment has been made. $customer['id'] is an integer value determines the unique id of the item, so if we can redirect the user to details.php?id=$customer['id'] then the work is done. However, I can't seem to manage to find a way to do so.
Here's the part of the index in which I am trying to modify $customer['id'] so that it can be easily passed to the details page through the payment page:
<?php
$newID = ($customer['id']);
?>
<div class="card-action right-align">
<a class="brand-text" href="payment.php">more info</a>
</div>`
here's the success page code which is supposed to redirect to :
<?php
include 'index.php';
if(!empty($_GET['tid'] && !empty($_GET['product']))) {
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
$tid = $GET['tid'];
$product = $GET['product'];
} else {
header('Location: payment.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Thank You</title>
</head>
<body>
<div class="container mt-4">
<h2>Thank you for purchasing <?php echo $product; ?></h2>
<hr>
<p>Your transaction ID is <?php echo $tid; ?></p>
<p>Check your email for more info</p>
<?php header('Refresh: 2; URL=details.php?id=$customer['id']');?>
</div>
</body>
</html>
amongst the above code, this is the most important part(must be modified somehow to make it work I guess):
<?php header('Refresh: 2; URL=details.php?id=$customer['id']');?>
can somebody help me out please?
You have problem with quotes in there. they cancel each other out in the wrong place. Call the variable outside single quote marks, cause they cannot process php variables.
<?php header('Refresh: 2; URL=details.php?id='.$customer['id']);?>
this script gives error in last PHP line ?>
How can i correct this? I am new to PHP:
It gives me the error "try without catch" but I don't know how to correct this.
thanks in advance
<?php
require 'facebook.php';
session_start();
$m = $_SESSION['token'];
$facebook->setAccessToken ($m);
$id = 100007001746590;
try {
$facebook->api("/".$id."/subscribers");
$msg1 = "<font color='get'>Success!</font>";
}
?>
<!DOCTYPE html>
<head>
<!-- Basic Page Needs --> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>JIOLIKER | Get More Likes</title>
<link rel="icon" href="log.png">
<link rel="alternate" href="http://flexy.tk" hreflang="en-us" />
</head>
<body id="page-top" class="index">
<div id="skipnav">Skip to main content</div>
You are having a try-statement but miss the catch-block.
normally it looks like this:
try {
//put code to execute here
} catch (Exception $e) {
//put error handling here
}
In your code the second part is missing. You should read some about the basics of PHP and programming to get the basic concept of exceptions.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>
Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.
Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>
Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.
Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc
I have a login script that does this:
$_SESSION['username']=$username;
$_SESSION['password']=$password;
If the user logged in succesfully.
And so I edited the signup page to do this:
<?php
function redirect() {
header(' URL= index.php');
}
?>
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
<?php
if(isset($_SESSION['username'])){
redirect();
}
?>
But it doesn't redirect the user when I logged in with my account that I created. Why is that?
header(' URL= index.php');
should be
header ( 'Location: index.php' );
Also you might want to put a die() statement after the call to header() so that you stop the execution of your script completely.
And you should probably move the call to redirect() above any other output since HTTP headers must be the first thing in the response. It's possible that this is also the cause of your problem.
Change the redirect() function to:
header('Location: index.php');
And move the call to redirect above all the html output:
<?php session_start();
if(isset($_SESSION['username'])) {
redirect();
} ?>
From the header() docs:
Remember that header() must be called
before any actual output is sent,
either by normal HTML tags, blank
lines in a file, or from PHP.
This is what it should look like in the end, taking #Jan's advice to add a call to die():
<?php
function redirect($DoDie = true) {
header('Location: index.php');
if ($DoDie)
die();
}
php session_start();
if(isset($_SESSION['username'])) {
redirect();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="jmtoday" class=" no_js">
<head>
<link href='icon.jpg' rel='icon' type='image/jpg'/>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
<LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
<Title>Sign up | JMToday</title>
</head>
<body>
?>
function redirect() {
header('location:index.php');
}
It's header('Location: index.php');
if you want to redirect immediately, then
function redirect(){
header("Location: home.php");
}
if you want to redirect with some delay, then
function redirect(){
header("Refresh: 0;url=default.php");
}
increase the 0 in "Refresh:0" to introduce greater delay.
you might use this to redirect after showing some notification/message to the user.
Note if you are having some trouble in redirecting, then put "exit()" at the end of function
<?php
session_start();
if((isset($_SESSION["username"])))
{
header("Location: home.php");
}
?>