how to stop direct access a page without login in php - javascript

I have login page, and after login I have user information related pages. so i don't want to direct access those pages from url. Instead of it will redirect to login page. how can I do that.
my sessions.php page code
<?php
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
?>

in your login page: if user successfully logged-in then set any session variable i.e. $_SESSION['userLogin'] = "Loggedin";
Then
each and every page check user loggedin or not by below code:
session_start();
if(empty($_SESSION['userLogin']) || $_SESSION['userLogin'] == ''){
header("Location: http://example.com/login.php");
die();
}

Related

Get Image captcha result after form submit from a site with login

I am a complete newbie to Web development.
There is a website with link such as https://example/login.asp with three fields of id, password and an Image Captcha. I am creating an API where the image captcha is automatically detected or taken from the session cookie after the submit button so the user doesn't need to type it.
<?php
echo '';
if(isset($POST['submit'])){
echo '<pre>';
echo print_r($_SESSION['imgCaptcha']);
echo '</pre>';
}
?>
<script type="text/javascript">
$(document).ready(function (response) {
console.log(response.getAttribute("imgCaptcha"));
})
</script>
I am getting a completely blank page. What should I do and where am I going wrong.

how to run a php code on clicking a hyperlink

I am working on a module, "who viewed your profile" in a social networking site project. When a logged in user clicks on hyperlink of a user, he must be directed to a dummy page called view.Information of user who has clicked link of other user and username of user whose link has been clicked is stored in a database table, views. I tried doing this by calling a function updatetable on click of the hyperlink and using this function, I want to send variables viewername and viewedname to view.php, which inserts a record into views table.
while($row=mysqli_fetch_array($res))
{
?>
<?php echo $row['username']; ?>
<?php
}
The above code displays links of all users and if a link is clicked, view.php page is opened where some php code has to be run.
My updatetable function is as follows:
<script type="text/javascript">
var viewedname,viewername;
function updatetable(viewedname,viewername)
{
$.post('view.php' { viewer_name:viewername, viewed_name:viewedname } );
alert(viewedname);
}
</script>
view.php code:
if ($_POST && isset($_POST['viewer_name']) && isset($_POST['viewed_name'])) {
$viewer_name = ($_POST['viewer_name']);
$viewed_name = mysql_real_escape_string($_POST['viewed_name']);
$con=new mysqli('localhost','root','','test');
if($con->connect_error) echo $con->connect_error;
$r=mysqli_query($con,"insert into views(viewer,viewed) values('$viewer_name','$viewed_name')");
}
Problems that I am facing:
alert is not being shown and insertion is not happening after view.php code.
Is there a mistake in the way I'm sending the viewer_name and viewed_name variables?
I am new to php. Please help me out! Thank you :)
Missing a (,) in your javascript post:
Replace with:
$.post('view.php', { viewer_name:viewername, viewed_name:viewedname });
PHP:
Its better doing it this way:
if ($_POST){
if(isset($_POST['viewer_name']) && isset($_POST['viewed_name'])) {
//Do your stuff
}
}

User Name display as after login

I want to display logging user name. But below given code working perfect but the same page only working. I want home.php to display.
Login.php
if($check_user>0)
{
$_SESSION['user_name']=$username;
echo "<script>window.open('home.php','_self')</script>";
}
Home page coding
<?php echo($_SESSION['username']); ?>
But this code undefined index error showing.
How can call session username in home page
You have undefined index because you setting $_SESSION['user_name'] and try to read $_SESSION['username']. Change your home page code to: <?php echo($_SESSION['user_name']); ?>

Avoid Logged in users to access index.php

I have a ticketing website and I want to avoid users from opening index.php after they logged in. When they are logged in, they are automatically redirected to dashboard.php. Because my Login page is my index.php file and I want to customize it for login only. I want to write some code like below in PHP or jQuery or JavaScript:
<?php
$call_user = $site_calls->call_user;
if ($call_user <> 0){
//redirect to ("dashboard.php");
}
?>
If someone already logged in , they are redirected to dashboard.php whenever they want to access to index.php page.
`Simplest code, working shown below. Set a session variable after login, check for that session variable in index.php, if its set, redirect to dashboard.php`
<?php
session_start();
if(isset($_SESSION["user"])) //change to your session variable
{
header("Location: dashboard.php");
}
?>

I want to load a gif image when user clicks on logout and then redirects to home page. i am trying to do using php

I have a page(era.php) which has a logout option. When user clicks on logout, it calls logout.php which destroys session and has header too index:
header('Location: index.php');
This all is working fine. But, i want some gif image to be loaded when user clicks on logout and then it redirects to index.
It is very quick now...actually i want to add some delay which shows some image saying Redirecting.. something.
I have done this and is working although:
When a user clicks on "logout" load.php is loaded:
Here it is:(i have passed Get request because if user tries to open load.php through url)
load.php
<?php
session_start();
if(!isset($_SESSION['user_id']) || !isset($_SESSION['user_name']) || !isset($_SESSION['user_right']))
header('Location: .');
if(isset($_GET['val']))
{
if($_GET['val']=='out')
{
?>
<html>
<head>
<title>Redirecting..</title>
</head>
<body>
<div style="margin: 250px 0px 0px 600px;">
<div>
<img src="img/301.gif">
</div>
</div>
</body>
</html>
<?php
header("refresh:5;url=logout");
}
}
else
header('Location:.');
?>
And, then logout.php destroys session and calls the index.
You can use a logout.php as a middleman, without header redirection and do any action in that page.
E.g. removing the current session, showing an html message and redirecting with javascript after x seconds.
Update: You can use the a meta tag to refresh the page. W3C page

Categories

Resources