PHP :Anyone know Session handling like Facebook page does? - php

Problem :
User is already logged in
so when I type
local.test_php.com/form/
It takes me to Login page. local.test_php.com/form/index.php
But i want it to take it to my home page instead.(http://local.test_php.com/form/home.php)
what i want is like what facebook does when we type facebook.com (already logged in before) we r redirected to the news feed ... but in my case i get redirected to Login page(even if i'm already logged in) so i have to login again or type the url to get to the home page
I don't know how to customize sessions to always redirect to home page whenever user is logged in. It should not take me to login page in any condition.
Home page looks like this:
<!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
<?php
session_start();
if(isset($_SESSION['user'])){
}else{
header("location: index.php");
}
$user = $_SESSION['user'];
echo "<br>";
echo "WELCOME ".$user." ";
echo "<br>";
echo "Do you want to ";
print 'logout';
echo " ?";
echo "<br>";
?>
</body>
</html>

I rewrote your code a littlebit, so it's more readable now.
First of all, when you want to use header function on your page, or start a session, you should do it before anyhing in the output buffer.
As you see, I removed the echo things, if you use an IDE as an editor, syntax highlighting help you to read the code, and be sure, it is valid.
The second thing, if you want to "remember" for user, when user closes all the browsers what stored the session variables, then you need to give a shot for cookies. But first, fix the code.
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("location: index.php");
}
?><!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
<?php
if (!empty($_SESSION['user'])) {
?>
<p>WELCOME <?php echo $_SESSION['user']; ?></p>
<p>Do you want to logout?</p>
<?php
}
?>
</body>
</html>

try this:
<?php
session_start();
if(isset($_SESSION['user'])){
}else{
header("location: index.php");
}
$user = $_SESSION['user'];
echo "<br>";
echo "WELCOME ".$user." ";
echo "<br>";
echo "Do you want to ";
print 'logout';
echo " ?";
echo "<br>";
?>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<head>
<title>My first PHP Website</title>
</head>
<body>
<h2 align="center">Home Page</h2>
the problem with your code is that you want the header function after html output

Related

I have to face session issue

I have to face session problem in my new server.
Session not working properly for this link
I have refreshed page not show session and then again refresh page with Ctrl+f5 then session show.
So please tell me this is a server issue or coding issue ??
have a coding issue than solution me any one.
visit link to see phpinfo() : http://brahmanparivar.com/phpinfo.php
index.php and display.php
<?php session_start(); ?>
<html>
<head>
<title>PHP session</title>
</head>
<body>
<? if(isset( $_SESSION['name'] ) ){
echo $_SESSION['name'];
} else{
echo "not have a Session";
} ?>
<br><br>
home display set Session Session Out
</body>
</html>
logout.php
<?php session_start(); ?>
<html>
<body>
<?php
if(isset( $_SESSION['name'] ) ){
session_destroy();
}else{
echo "not have a Session";
}
?>
<br><br>
home display set Session Session Out
</body></html>
set.php
<?php session_start();
$_SESSION['name']="Session Set"; ?>
<html>
<head>
<title>PHP session</title>
</head>
<body>
<? if(isset( $_SESSION['name'] ) ){
echo "Set a Session :: Session Set";
} ?>
<br><br>
home display set Session Session Out
</body>
</html>
in your logout.php page, you are missing a php open tag <?php.
Change this:
<? if (isset($_SESSION['name'])) {...
to this:
<?php if (isset($_SESSION['name'])) {...
I dont think short tag fixes his problem. When i ran your code it works perfectly.
Probably it's configuration issue.
Please read: How to get my session to write to apache
see if that fixes your problem for further trouble shooting
I would like to see the phpinfo();
also attach
<?php
session_start();
echo session_id();
?>
to every page I dont think your PHPSESSIONID is not being set. properly

How to give each a element his specific url - PHP

I working on a simple project, and basically what I am doing is that I am creating a simple page from admin panel and then displays it in another page using iframe...
So in the process of displaying the page names, I managed to display all the page names that I have in the database. However, when I try to give them a link so when the user click on them, it redirects them to the specific page. So if there is a page named: new york and he clicks on it, it goes to: /pages/newYork.php ...
The problem: When I give them a link, only 1 page link is given for all the pages. Example: If I had cities named: New York, Bangkok and Amsterdam, the link for all of those pages is only for New york.
Here is my code:
<?php
require_once '../connect.php';
session_start();
if(isset($_SESSION['loggedinAdmin'])){
}else{
header("Location: index.php");
}
// To display content:
$realDisplay = '';
$sql = mysql_query("SELECT * FROM cities");
while($row = mysql_fetch_assoc($sql)){
$displayName = $row['name'];
$realDisplay .= $displayName.'<br/>';
}
echo "<u align='center'>Page names:";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display Pages</title>
<style>
a{
text-decoration:none;
font-size:30px;
color:skyblue;
}
</style>
</head>
<body>
<div style="color:red;">
<a href="<?php echo 'pages/'.$displayName.'.php'?>">
<?php echo $realDisplay;?>
</a>
</div>
</body>
</html>
Any thoughts of how to solve this? Also, if there is a better way to do it then it would be great.
Thanks in advance for everybody :)
First, move your loop to the page where you are actually going to display the data.
Second, keep your code clean and readable by putting things where they actually belong. ie. <u align='center'>Page names: doesn't belong where you put it. It goes in the body.
Third, don't forget to properly close your tags. <u align='center'>Page names: has no closing tag.
Finally, don't over-complicate it. You don't need two different vars for the same data row.
<?php
require_once '../connect.php';
session_start();
if(isset($_SESSION['loggedinAdmin'])){
}else{
header("Location: index.php");
}
// To display content:
//$realDisplay = '';
$sql = mysql_query("SELECT * FROM cities");
//while($row = mysql_fetch_assoc($sql)){
//$displayName = $row['name'];
//$realDisplay .= $displayName.'<br/>';
//}
//echo "<u align='center'>Page names:";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display Pages</title>
<style>
a{
text-decoration:none;
font-size:30px;
color:skyblue;
}
</style>
</head>
<body>
<div align="center"><u>Page names:</u></div>
<div style="color:red;">
<?php while($row = mysql_fetch_assoc($sql)){
$displayName =$row['name']
?>
<?php echo $displayName;?><br>
<?php } ?>
</div>
</body>
</html>

php redirect when wrong password

is this right the code will redirect a person to the login page when they try to access it using without going into the login page
<?php
$pass = 'password';
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if ( $_POST["pass"] == $pass){
?>
Congrats you have log in!
<?php
}else{
header("Location: http://signin.com/");
}
?>
</body>
</html>
i ended up having a "Server error
The website encountered an error while retrieving http://www.test.com It may be down for maintenance or configured incorrectly."
You can't call header after you've already outputted some HTML. Do your password checks & redirect. above the HTML
Eg:
<?php
$pass = 'password';
if ( $_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
....
So the HTML will only show if they're successful.
You can't send a header() after any output to the user:
<?php
$pass = 'password';
if ( $_POST["pass"] == $pass)
{
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
<?php
}
else
{
header("Location: http://signin.com/");
}
?>
Something like this would work better:
<?php
$pass = 'password';
if ($_POST["pass"] != $pass){
header("Location: http://signin.com/");
exit;
}
?>
<html>
<head>
<title></title>
</head>
<body>
Congrats you have log in!
</body>
</html>
You need to check if the user is logged in. If not, redirect and exit. If so, display the message.
Put ob_start(); at the top and ob_end_flush(); and that might fix it.
You can't output html before make a redirect with header. Code all logic before:
<?php
$pass = 'password';
if ($_POST["pass"] == $pass)
{
$message = "Congrats you have log in!";
}
else
{
header("Location: http://signin.com/");
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $message; ?>
</body>

HTTP redirect with header function does not work

header("Location: …" ); does not seem to be working:
<?php
session_start();
if (isset($_SESSION['user'])){
?>
<html>
<head>
<title>
Admin Panel
</title>
</head>
<body>
</body
</html>
<?php
} else {
header("Location: http://echo2.site40.net/cms/admin/login.php" );
}
?>
Seems to be working if I exactly copy paste the code...
Still look for the empty white-spaces before start of the <?php

"HTTP_USER_AGENT" not working. nothing is displayed in my browser i.e. firefox

<?php
$agent=getenv("HTTP_USER_AGENT");
if(preg_match("/MSIE/i", "$agent")){
$result="You are using Microsoft Internet Explorer.";}
else if (preg_match("/Mozilla/i", "$agent")){
$result= "You are using Firefox.";}
else {$result = " you are using $agent.";}
?>
<html>
<head>
<title>Browse Match Results</title>
</head>
<body>
<?php "<p>$result</p>";?>
</body>
</html>
Might I venture a guess, and suggest that:
<?php "<p>$result</p>";?>
Should be:
<?php echo "<p>$result</p>";?>
<?php echo "<p>$result</p>";?>

Categories