How to syntax the session_start(); if in a separate php file - php

I have a contact form that links to an external php file (because I have a single-scrolling page with all content on it, ran by jQuery and it wouldn't make sense to have it all as a .php file as would be the case with a single, separate contact page).
My question is, since I have a captcha to protect against spam and such, I have to include the session_start(); in both the contact php file and the captcha. My question is would I syntax it in the contact.php file as:
<?php session_start(); ?>
and then have more php here, i.e. - my contact form script
?>
or just have it be the first line as:
session_start();
after the initial php tags?
Thanks for the help.

You need to include session_start(); at top of all your php codes to apply a session.
<?php
session_start();
$user='New User';
$_SESSION['user']=$user;
//Print this line on another php code you will get the same username
echo "Welcome $_SESSION['user']";
?>

Question is not clear. You should probably try this code at the top of the page.
<?php
#session_start();
?>
check this link
http://www.php.net/manual/en/function.session-start.php
or this Load session_start() only if session does not exist?

Related

Storing Session information and linking to new page on click

I hope this isn't stupidly simple. I am completely new to web dev.
I have list items that I styled as buttons.
I want to be able to link to a new page as well as store some information when the list items are clicked. I want to be able to store which list item was clicked in a Session variable.
How do I accomplish this/ is there a better way to accomplish the same thing?
Sessions Step By Step
1- Defining session before everything, Ideally do it on very top of the document so no output is being exposed etc like this
<?php
session_start();
?>
2 - Set your session inside a page and then you have access in that page. For example this is page 1.php
<?php
//This is page 1 and then we will use session that defined from this page:
session_start();
$_SESSION['danish']='danish';
?>
3- Using and Getting session in 2.php
<?php
//In this page I am going to use session:
session_start();
if($_SESSION['name']){
echo 'Your name variable Is Here! :) ';
}
?>
In short its like you assign session variable in a page and then using same declarative syntax instead of assigning you call the variable and PHP do the magic to check if that session variable was created and hold the value so, in short i can write my code like this
First Page
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Second page
<?php
session_start();
echo $_SESSION['myvar'];
?>

PHP - combining php with html to display logged in name

when the user logs in, their name gets saved into a session variable called 'name'. All pages should display the user's name if logged in, or a link to log in if the user is not logged in. I have tried the following code in the pages that require this feature, however I am just shown a white screen and I cant figure out where the error may be.
<?php if ( isset( $_SESSION['name'] )){ ?>
<p>Welcome back <?php echo $_SESSION['name'];?></p>
<?php} else{?>
Login <?php } ?>
EDIT: session has been started on the pages
Seems an erro at Line 3. You have missed spaces after open php tag
<?php } else{ ?>
^ ^
Have you added session_start() at start of your page?
This problem could be caused by not using session_start() before accessing $_SESSION[''].

How to transfer php session variables from a page to an iframe inside the page

So I have a webpage with session variables, for example one variable is:
$_SESSION['name'] = 'testname';
I have an iframe within the page, and want to use this session variable in it. For a start, I am trying to just do this:
echo $_SESSION['name'];
I have not found a way to transmit the session variable into the iframe page.
INFORMATION (IMPORTANT!):
I have used session_start(); on both the main page and the iframe page, and I have also tried using session_write_close(); on both pages. The pages are in the same domain. Please answer. Thank you in advance!
What ever page is referenced in the iframe, if it exists on your the same domain will have access to the same session information on the hosting site so long as that page has session_start() called on it.
Example.
page1.php
<?php
session_start();
$_SESSION["HELLO"] = "WORLD";
?>
<html>
<iframe src='page2.php'/>
</html>
page2.php
<?php
session_start();
echo "HELLO ".$_SESSION["HELLO"]; // will output HELLO WORLD
This is only true for sites that have access to the same cookies and the same session store.

Using php session variable in different pages

I created a session variable in a php page 'session1.php' as follows
<?php
session_start();
$_SESSION['name']="piklu";
?>
now I want to access the session value in another page say 'session2.php'.i need suggestion for that.
what I have done in 'session2.php' is as follows.
<?php
include('session1.php');
echo $_SESSION['name'];
?>
The above code is working but only when session1.php and session2.php doesn't contain any html tags.if it is then all html tags from 'session1.php' will be copied to 'session2.php'. Is there any other way to do that?
You don't need to include all of session1.php. instead, just call session_start() at the start of session2.php.

Javascript alert instead of redirect in PHP mail script

Thanks to Col. Shrapnel I am using a VERY basic PHP script to send emails. The form is located here.
This is the script:
<?php
mail('JBIRD1111#gmail.com','Live Date Submission',implode("\n\n",$_POST));
header("Location: thankyou.html");
?>
When a user submits the form, they are redirected to a thankyou.html page. I want to edit the code to display a javascript alert, instead of a redirect. I don't have much PHP knowledge, so how would I edit this code to return a alert instead of a redirect?
Actually, if you want to send a Javascript alert instead I would recommend using some basic jQuery work. I would also take a look at the AJAX section of the documentation.
Otherwise you can inset some javascript in the original form page.
session_start();
$_SESSION['message'] = "<script>alert('Thank you so very much! You rock!');</script>";
header("Location: originalformpage.html");
and on the original form page
session_start();
if(isset($_SESSION['message']))
{
echo($_SESSION['message']);
unset($_SESSION['message']);
}
Replace the header line with:
print("<script>alert('Thank you so very much! You rock!');</script>");
Not tested but should work.

Categories